Problems using .Find for dates in formulas

Dan_R

New Member
Joined
Jul 15, 2015
Messages
10
Hi,
I'm hoping someone will be able to help me out with a problem that's driving me mad at the moment. I have a macro which I use to update some daily statistics by pulling data from one file, finding the relevant date in column A, then pasting a row of date next to it. The spreadsheet already has the dates typed in for the whole year, which is ok, but not a very good solution as I'll have to duplicate this for several years on multiple worksheets, it would be much better if I could have the dates as a formula. When I try to put the date in column A as a formula instead of a manually entered date, the macro cannot find it and I get the error 'Run-time error '91' Object variable or With block variable not set'. I know that I've seen other similar posts on here but I can't find anything that makes it work. I know it shouldn't matter what format the date is in as excel recognises it as a date whatever, and I've tried changing the format several times and it dosen't seem to make a difference - but it has to be just dd for this sheet so although it displays '01' for example, the actual cell value is '01/07/2015', here's an example of how the spreadsheet looks:-

July
Hour
Date
12345678
9
101112131415161718192021222324
01
20.2
23.8
26.428.229.130.031.132.0
32.533.232.732.230.829.828.728.027.427.226.526.325.924.421.620.2
0221.6
24.2
27.728.6
29.131.131.632.4
33.733.732.932.631.529.52928.528.427.927.326.9272625

<tbody>
</tbody>
And here's the bits of code which I think are relavant:-

Dim MyDate, DataDate As Date
Dim MyDay, sDate As String

MyDate = Now
DataDate = Format(MyDate - 1, "dd/mm/yy")

'the bit to select which date gets imported

sDate = InputBox("Choose date (dd/mm/yy), "Date to import", DataDate)

If sDate = "" Then
MsgBox "Cancel Pressed" & Chr$(13) & "No data imported"
GoTo THEEND
End If

If IsDate(sDate) Then
DataDate = Format(sDate, "dd/mm/yy")
GoTo DATESELECTED
Else
MsgBox "Bad date entry, please try again"
DataDate = Format(MyDate - 1, "dd/mm/yy")
GoTo SELECTDATE
End If

-------
'the find bit
Set Rng = Range("A:A").Find(DateValue(DataDate), LookIn:=xlFormulas, lookat:=xlWhole)
Rng.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues

Sorry, I'm new here so if there's any obvious bits of information that I've missed out please let me know - otherwise thanks in advance if anyone can help me out.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi and welcome to the MrExcel Message Board,

You could try MATCH instead of FIND:

=MATCH(DATEVALUE("1/2/15"),A:A,0)

or in VBA:
Code:
Sub myMatch()

    Dim rc As Variant
    Dim dt As Date
    dt = Date
    
    rc = Application.Match(CLng(dt), Range("A:A"), 0)
    If IsNumeric(rc) Then
        Debug.Print "Row number is " & rc
    Else
        Debug.Print "Date not found"
    End If

End Sub
 
Upvote 0
Thanks, that's great - it's working now
I'd tried to use the Application.Match and CLng before, but got a bit confused with it. As I know that the dates are always going to be in column A I've just used:

Range("B" & rc).Select
Selection.PasteSpecial Paste:=xlPasteValues

to paste he values in the rigt place
Thanks again :)
 
Upvote 0

Forum statistics

Threads
1,216,300
Messages
6,129,978
Members
449,547
Latest member
RichieRichJPN

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