Find function finds the incorrect date.

cosmarchy

New Member
Joined
Nov 11, 2009
Messages
13
I've tried using the code below, in various formats as you can see from the commented out code, to try and find the address of the cell in the first row which contains the current month of the year.

VBA Code:
Private Sub Find_test()

    Dim dteTodaysMonth As Date

    dteTodaysMonth = Format(Now, "mmmm - yyyy")
    'dteTodaysMonth = DateSerial(Format(Now, "yyyy"), Format(Now, "mm"), 1)
    
    With Worksheets("Sheet1").Rows(1)
        'Debug.Print .Find(What:=dteTodaysMonth, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Address
        'Debug.Print .Find(What:=DateValue(dteTodaysMonth), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Address
        Debug.Print .Find(What:=DateSerial(Format(Now, "yyyy"), Format(Now, "mm"), 1), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Address
    End With

End Sub

All the months in the first row display as 'January - 2020', 'February - 2020' etc.

However, the code always displays the date of the cell with 'December - 2020' in it and not February as you'd expect. Now I'm sure it is an issue to do with the way that date/time is stored but I'm at a loss as to how to fix this issue.

Any help is appreciated. Thanks
 
Ok, that's fine, as it's a real date. Unfortunately I don't know what to suggest as this works for me
VBA Code:
Debug.Print .Find(What:=DateSerial(Format(Now, "yyyy"), Format(Now, "mm"), 1), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Address
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I think getting rid of the merged cells that the dates are in would be a good start. VBA and merged cells do not have a happy relationship because VBA does not know which cell in the merger it us supposed to interact with. One way to handle that is to not use merged cells in the data base and if the boss wants his data formatted, the extract it from the database into a formatted report to give to the boss.
 
Upvote 0
I usually convert dates to long such as:

VBA Code:
Dim dteToday As Long, dteMatch

dteToday = DateSerial(Year(Now), Month(Now), 1)

With Worksheets("Sheet1")
    dteMatch = Application.Match(dteToday, .Rows(1), 0)
    If IsNumeric(dteMatch) Then Debug.Print .Cells(1, dteMatch).Address
End With
 
Upvote 0
@JLGWhiz Real shame to have to do so... for me it ought to return a cell even if it isnt the correct one due to the merge. It seems very wrong of Excel to not find the very date I am sitting there staring at.

More worrying though is that Fluff has this working and I cannot and we are using the same code!!
 
Upvote 0
@JLGWhiz Real shame to have to do so... for me it ought to return a cell even if it isnt the correct one due to the merge. It seems very wrong of Excel to not find the very date I am sitting there staring at.

More worrying though is that Fluff has this working and I cannot and we are using the same code!!
I don't believe @Fluff is using merged cells in her test set up. But I could be wrong.
 
Upvote 0
Im not sure merged cells matter.
They might not, depending on the code or formula and whether it is doing anything more than looking for a value in that range of cells, But it can be a real pill if the code tries to return a value, or use those cells in a filter, etc. VBA will not execute many commands on merged cells. Even if it returns the value, it returns it for the left top cell of the merged group and could be completely misleading for correlating any data in columns underneath the merged range.
 
Upvote 0
Whilst I agree that merged cells are a bad idea & should be removed, I did test the OP's code using merged cells & it worked for me with UK dates.
 
Upvote 0

Forum statistics

Threads
1,216,087
Messages
6,128,740
Members
449,466
Latest member
Peter Juhnke

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