I have a set of data where every third column is the same. I want to leave only the first column and other which are the same must be deleted.
At first I tried this
Reverse the direction. Start deleting from the right. I think you know how to modify your code
I upvoted Mattboy's code as the cleanest
It is possible to avoid the range loop and use an array as you suggest, although I post this more for kicks as the array generation is tricky
Uses Is it possible to fill an array with row numbers which match a certain criteria without looping?
Sub OneinThree()
Dim ws As Worksheet
Dim rng1 As Range
Dim x As String
Set ws = ActiveSheet
Set rng1 = Cells(1, ws.Cells(1, Columns.Count).End(xlToLeft).Column - 2)
x = Join(Filter(Application.Evaluate("=IF(MOD(column(A1:" & rng1.Address & "),3)=0,address(1,column(a1:" & rng1.Address & ")),""x"")"), "x", False), ",")
If Len(x) > 0 Then ws.Range(x).EntireColumn.Delete
End Sub
You can go backwards like this. Also, you don't need to select the column before deleting, you can simply delete it right away.
For i = ((LastColumn \ 4) * 4) To 4 Step -4
ws.Columns(i).Delete Shift:=xlToLeft
Next i