Help! My Macro is only deleting every other column- how to work from right???

jmthompson

Well-known Member
Joined
Mar 31, 2008
Messages
966
This macro is skipping columns and I know it's because I should work from the last column back, but I don't have the brain power to fix it, help?


Code:
Sub MyMonth()
TheDate = Format(Now, "mmm yy")
LastColumn = Range("IV11").End(xlToLeft).Column
Dim c As Range
        For Each c In Range([P1], Cells("1", Columns.Count).End(xlToLeft))
            If c <> TheDate Then c.EntireColumn.Delete
        Next c

End Sub
 

Some videos you may like

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,014
Any time you delete rows or columns you need to work backwards (right-to-left; bottom-to-top). Generically
Rich (BB code):
for i = rngSomeRange.Rows/Columns.Count to 1 Step -1
     '// «code that does deletion»<DELETION>
Next i
 

Watch MrExcel Video

Forum statistics

Threads
1,122,566
Messages
5,596,898
Members
414,109
Latest member
Coops01

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
Top