Did I drive my De Lorean to work? Why does this macro think it is January 2005?

jmthompson

Well-known Member
Joined
Mar 31, 2008
Messages
966
Good morning,
I have created the Macro below to delete all columns from P-last column whose header is not Month-Year (this month should be June-08). I've tested this code with columns whose header is text and with columns whose header is entered as a date and formatted date Month-yy and a combination of the two, either way it deletes all columns from P to the last column. I've tried stepping through the code, and when I hover over MyMonth in the If statement, January appears. When I hover over MyYear, 2005 appears.

Any ideas?


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

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Change

MyMonth = Format(Month(Now), "mmmm")
MyYear = Format(Year(Now), "yy")

to

MyMonth = Format(Now, "mmmm")
MyYear = Format(Now, "yy")
 
Upvote 0
You don't need the Year or the Month function in there ie:

Code:
MyMonth = Format(Now, "mmmm")
MyYear = Format(Now, "yy")

Both of these could additionally be shortened to:

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

End Sub

VBA and Excel treat dates as days from 1/1/1900. Month(Now) returns 6. If you try and format that as "mmmm", VBA tries to interpret 6 as a date, which it treats as 6 Jan 1900. For year(Now) you get 2008, then formatted as year you get 2008 days from 1/1/1900 which is 30 June 1905.
HTH
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,585
Members
448,972
Latest member
Shantanu2024

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