audioboxer
New Member
- Joined
- Aug 13, 2012
- Messages
- 23
Hello,
I am having an issue with my IF statement below involving removing columns and that it ends up not removing all the columns it should. When I do a debug.print it finds the correct columns to remove however when it actually runs it's not removing them all. I figured out it's because as it deletes them, the columns shift. I cannot figure out how to properly form this to run correctly.
I am having an issue with my IF statement below involving removing columns and that it ends up not removing all the columns it should. When I do a debug.print it finds the correct columns to remove however when it actually runs it's not removing them all. I figured out it's because as it deletes them, the columns shift. I cannot figure out how to properly form this to run correctly.
Code:
For Each x In Range(Cells(1, 1), Cells(1, lastCol))
match = 0
For Each y In KeepCols
If x = y Then
match = match + 1
Else
End If
Next y
If match = 0 Then
'Debug.Print x & " Column Header " & " which is " & x.EntireColumn.Address & " Delete"
x.EntireColumn.Delete
Else
End If
Next x