Unable to find match using Match

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
What could be possible reasons why this code is failing to find a match? I get an error (Unable to get the Match property of the WorksheetFunction class) with the line in red.

The value for date_inq = 2024-02-01
In the worksheet (ws_cupe), cell B39 has a value of 2024-02-01 (formatted as 02-Feb-24)

Rich (BB code):
Sub Button1_Click()
    Dim date_today As Date
    Dim date_inq As Date
    Dim r_dateinq As Double
    
    date_today = Now
    date_inq = Date + 1
    
    Debug.Print date_today
    Debug.Print date_inq
    
    With ws_cupe
        r_dateinq = Application.WorksheetFunction.Match(date_inq, .Columns(2), 0)
    End With
    
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi,
untested but see if this update to your code resolves your issue

VBA Code:
Sub Button1_Click()
    Dim date_today  As Date
    Dim date_inq    As Long
    Dim r_dateinq   As Variant
    
    date_today = Now
    date_inq = Date + 1
    
    Debug.Print date_today
    Debug.Print date_inq
    
    r_dateinq = Application.Match(date_inq, ws_cupe.Columns(2), 0)
    
    If Not IsError(r_dateinq) Then
        
        'do stuff
        
    Else
        MsgBox date_inq & Chr(10) & "Record Not Found", 48, "Not Found"
        
    End If
    
End Sub

Dave
 
Upvote 1
Solution
Hi Dave ... that appears to have worked. Thank you.
Not quite sure why, I'm assuming the data types were not what I thought?
 
Upvote 0
Hi Dave ... that appears to have worked. Thank you.
Not quite sure why, I'm assuming the data types were not what I thought?

Searching for dates can prove tricky but a date is a number & passing it to a variable declared as long data type works most of the time
You will note that I also changed the data type for Variable r_dateinq to variant. This is so the error value can be handled if search value not found.

Appreciate feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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