In the following code I am trying to use the For Each...Next statement to identify cells in a range that contain a particular string. Then I want to cut each entire column and move to Columns(1). There are usually at least 3 or 4 of these columns. At the moment the code is moving all columns where the header contains this string apart from one that it leaves behind.
I am struggling to correct this so that all columns with the string in the header are moved. Please can anyone identify the problem and correct for me?
I am struggling to correct this so that all columns with the string in the header are moved. Please can anyone identify the problem and correct for me?
Code:
Dim lcol As Long
Dim AttStr As String
lcol = Range("A1").End(xlToRight).Column
AttStr = "Attachment"
For Each Mycel7 In Range(("G1"), Cells(lcol))
If InStr(Mycel7.Value, AttStr) > 0 Then
Mycel7.EntireColumn.ColumnWidth = 18
Mycel7.EntireColumn.Cut
Range("A:A").Insert Shift:=xlToRight
End If
Next Mycel7