My code below looks through a list of keywords in column 37, in rows 5 to 17, in a different sheet called "Weights".
For columns that do not contain these keywords, delete them.
My problem is that it is looking for exact matches, so I need to place some wildcards or adjust the below to include partial matches.
For example, if a keyword is "Open", then the column containing "Open & closed" would be deleted, which is not what I want.
How is best to go about this?
Sub DeleteUneededColumn()
Dim rng As Range, rngcol As Range
Dim findstring As Variant
With Sheets("Weights")
findstring = .Range(.Cells(5, 37), .Cells(17, 37))
End With
For Each rngcol In Range("A:CZ").Columns
myVal = 0
For i = LBound(findstring) To UBound(findstring)
myVal = myVal + Evaluate("=IF(COUNTIF(" & rngcol.Address & ",""" & findstring(i, 1) & """)>0,1,0)")
Next
If myVal = 0 Then
If Not rng Is Nothing Then
Set rng = Union(rng, rngcol)
Else
Set rng = rngcol
End If
End If
Next
If Not rng Is Nothing Then rng.Delete
End Sub
The following code should work for you...
The sub takes the keywords in the Sheet("Weights") and adds them to an array, then loops through the array looking for each term in the destination range. It will them loop through the destination range and remove any columns that don't intersect with the union of all the found search ranges