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
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
The Find Method always uses a text comparison. If your formatting is like January - 2020 and so on, you want to look for a match in the .Text value of each cell, not in the serial date number. See if this works for you:
VBA Code:
Sub Find_test()
    Dim M As String, Y As String, dteTodaysMonth, Fnd as Range
    M = MonthName(Month(Date))
    Y = Year(Date)
    
    dteTodaysMonth = CStr(M & " - " & Y)
'    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
        
        Set Fnd = .Find(dteTodaysMonth, LookIn:=xlValues)
        If Not Fnd Is Nothing Then
            MsgBox Fnd.Address
        Else
            MsgBox "Can't find today's date in row 1"
        End If
    End With

End Sub
 
Upvote 0
If your entries in row 1 are actual date type dates, then maybe this:
Try chaning you LookIn:= parameter;
Code:
Debug.Print .Find(What:=dteTodaysMonth, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Address[/Code[
 
Upvote 0
Thanks for your replies guys but unfortunately neither @JoeMo nor @JLGWhiz code actually finds the correct date.

With Joemo's code, I get the "Can't find today's date in row 1" message and JLGWhiz's code I get the 'object variable or with block variable not set' error.
 
Upvote 0
You're code works for me as long as row 1 contains actual dates like 01/01/2020, 01/02/2020 etc. regardless of date format in the cell.
 
Upvote 0
@Fluff ****, that's a bit annoying
Here is what I get from the JoeMo's and JLGWhiz's respective code:
 

Attachments

  • JLGWhiz Code.PNG
    JLGWhiz Code.PNG
    97.7 KB · Views: 9
  • JoeMo Code.PNG
    JoeMo Code.PNG
    92.4 KB · Views: 7
Upvote 0
Firstly, I would advise against using merged cells, as they will only cause you problems & grief.
With your code I get $BK$1 what do you get?
Also where are you UK, US, Australia, or ?
 
Upvote 0
@Fluff - I dont get anything other than the 'object variable or with block variable not set' error or the 'cant find todays data' message box...

I'm based in the UK.
 
Upvote 0
What exactly do you have in row 1? If you select the row & change the format to "General", what do you see?
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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