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
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,022
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,602
Messages
5,832,664
Members
430,153
Latest member
Javid_P

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