VBA .find Method on Date Values

dhancy

Board Regular
Joined
Jul 10, 2013
Messages
120
Office Version
  1. 2016
Platform
  1. Windows
Is there a "trick" to using the .find method in VBA to find a Date value?

I have always used something like this to find string values:

Code:
Set x = ws.Rows("1:1").Find(varString, LookIn:=xlValues)

where varString is a character string.


It works great. However, when searching for a date value, it's not working the same way.

I've tried many things:

- declaring varString as a date value
- search for CDate(varString)
- using various format strings on the date

Any other suggestions are greatly appreciated. Thanks!


Dennis
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Thank you !!!!

I've been looking at this all morning, and getting nowhere.

I used both the DateValue function to convert to a date, and also changes xlValues to xlFormulas.

It works great! THANK YOU !!!!
 
Upvote 0
You are welcome! Glad I was able to help.:)
 
Upvote 0
Check out the "Dates in the FIND Method" section at the bottom of this link here:
VBA Procedures For Dates And Times

Thank you for the post. I am new to Excel VBA. After struggling so many times to search a date in a range, I came across your post.

The code works smoothly, if the date I want to search is in the range. If the date I am trying to search is not in the range I get the Run-time error 91 message - Object variable or With block variable not set. Would you please advise how to fix it? Thank You.

My code:
Sub FindCell()


Dim FoundCell As Range


Set FoundCell = Range("A1:A15").Find(what:="10/2/2018") 'date in US format


Debug.Print FoundCell.Address
Debug.Print FoundCell.Value


End Sub
 
Upvote 0
Try
Code:
Sub FindCell()


    Dim FoundCell As Range


    Set FoundCell = Range("A1:A15").Find(what:="10/2/2018")    'date in US format

    If Not FoundCell Is Nothing Then
Debug.Print FoundCell.Address
Debug.Print FoundCell.Value
    Else
        Exit Sub
    End If
End Sub
 
Upvote 0
Id use this to replace your Set line:

Code:
myFind = DateSerial(2018, 10, 2)
Set FoundCell = Range("A1:A15").Find(What:=myFind, LookAt:=xlWhole)
 
Upvote 0
Try
Code:
Sub FindCell()


    Dim FoundCell As Range


    Set FoundCell = Range("A1:A15").Find(what:="10/2/2018")    'date in US format

    If Not FoundCell Is Nothing Then
Debug.Print FoundCell.Address
Debug.Print FoundCell.Value
    Else
        Exit Sub
    End If
End Sub

Thanks Mark. Works beautifully.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,823
Members
449,049
Latest member
cybersurfer5000

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