Please help modify code delete columns macro

jmthompson

Well-known Member
Joined
Mar 31, 2008
Messages
966
How could I change this code to find the cell = MyMonthandYear and delete all columns to the left of that column up to and including column "P"? Basically, in the range from P-last column, I want to keep the column with header that matched MyMonthandYear and all columns to the right of that column.

Code:
Sub MyColumn()
MyMonthandYear = Format(Date, "mmmm-yy")
LastColumn = Range("IV1").End(xlToLeft).Column
For i = LastColumn To 16 Step -1
If (Cells(1, i).Value) <> MyMonthandYear Then
Cells(1, i).EntireColumn.Delete
End If
Next i

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This seems to work

Code:
Sub MyColumn()
MyMonthandYear = Format(Date, "mmmm-yy")
Dim MyMonth As Range
LastColumn = Range("IV1").End(xlToLeft).Column
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = LastColumn To 16 Step -1
If (Cells(1, i).Value) = MyMonthandYear Then
Cells(1, i - 1).EntireColumn.Delete
End If
Next i
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,235
Members
449,092
Latest member
SCleaveland

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top