I made a VBA function that is supposed to do just this. Based on my other function of similar nature. Not elegant but does the job.
Public Function FINDHEADERWHERESUBSTRINGFITS(Target As Range, Condition As String)
Dim rng As Range
NumCols = Target.Columns.Count 'counts how many header values we can choose of
Dim Headers() 'defines separate arrays for headers and values (turned out to be obsolete, see variable x)
ReDim Headers(1 To NumCols)
Dim ValuesArr()
ReDim ValuesArr(1 To NumCols)
HeaderRow = Target.Row 'row in which headers are located
LastRow = HeaderRow + Target.Rows.Count - 1 'last row with values
FirstColumn = Target.Column 'first column with values
LastColumn = FirstColumn + Target.Columns.Count - 1 'last column with values
For k = FirstColumn To LastColumn 'for each column
i = i + 1 'set array position
For Each rng In Range(Cells(HeaderRow, k), Cells(LastRow, k)) 'for each value
If rng.Row <> HeaderRow Then 'I mean value, not header
If InStr(Condition, CStr(rng.Value2)) > 0 Then Headers(i) = Cells(HeaderRow, k).Value2 'if it's a substring of the condition then set the corresponding header
End If
Next
Next
FINDHEADERWHERESUBSTRINGFITS = Replace(Replace(Join(Headers, ","), ",,", ","), ",,", ",")
End Function
Back, with more commas* than ever before, the ultimate URL finder**.
*sorry dude, you dun' said formatting was irrelevant
**restrictions apply, see comments
Public Function FINDHEADERWHERESUBSTRINGFITS(Target As Range, Condition As String)
Dim rng As Range
HeaderRow = Target.Row 'row in which headers are located
LastRow = HeaderRow + Target.Rows.Count - 1 'last row with values
FirstColumn = Target.Column 'first column with values
LastColumn = FirstColumn + Target.Columns.Count - 1 'last column with values
NumCols = Target.Columns.Count 'counts how many header values we can choose of
NumCells = Target.Cells.Count - (LastColumn - FirstColumn + 1) 'counts how many URLs we can choose of
Dim Headers() 'defines separate arrays for headers and values
ReDim Headers(1 To NumCols)
Dim ValuesArr()
ReDim ValuesArr(1 To NumCells)
For k = FirstColumn To LastColumn 'for each column
i = i + 1 'set array position
For Each rng In Range(Cells(HeaderRow + 1, k), Cells(LastRow, k)) 'for each value
If rng.Row <> HeaderRow Then 'I mean value, not header
If InStr(CStr(rng.Value2), Condition) > 0 Then
Headers(i) = Cells(HeaderRow, k).Value2 'if it's a substring of the condition then set the corresponding header
j = j + 1 'increases the array position counter by one (not to overwrite the previous entry)
ValuesArr(j) = CStr(rng.Value2) 'inserts URL to array position
End If
End If
Next
Next
FINDHEADERWHERESUBSTRINGFITS = Replace(Replace(Join(Headers, ","), ",,", ","), ",,", ",") & "; " & Replace(Replace(Join(ValuesArr, ","), ",,", ","), ",,", ",")
End Function