VBA For Loop Not Matching Dates

JessP

New Member
Joined
Jan 11, 2018
Messages
23
In part of some code I'm working on, I'm trying to find the cell containing the first day of the previous month, then hiding everything from column U up to that column.

I've attempted a number of different methods, but nothing I've tried seems to work - it doesn't give an error, just skips over the for loop. Here's the relevant section of my most recent code - sorry, I didn't save all the attempts.

Code:
Dim cel as Range
Dim dtStart as Date
dtStart = DateSerial(Year(Date), Month(Date)-1,1

For Each cel In Range("U3:GA3")
    If cel.Value = dtStart Then
        cel.Offset(0, -1).Select
        Range(Selection, "U3").EntireColumn.Hidden = True
        Exit For
    End If
Next cel

In case this is affecting it, the dates in the file are formatted as =text(date(2017,3,1),"mmm"), but they should still be saved as a serial, right?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Welcome to the Board!

In case this is affecting it, the dates in the file are formatted as =text(date(2017,3,1),"mmm"), but they should still be saved as a serial, right?
No, the TEXT function makes the value a string, not a date. If you want it to be a date, but just show the month abbreviation, use this:
Code:
=date(2017,3,1)
and then just apply the Custom Date format to the cell of "mmm".
Then it will be a date in the format that you want.
 
Upvote 0
Thanks! Works perfectly - I probably should have realized it wasn't the code's fault by the time I'd written half a dozen that should have worked. I appreciate your help.
 
Upvote 0
You are welcome.
Glad I was able to help!:)
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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