Loop through Worksheets to find row of current date

Paul

Board Regular
Joined
Apr 2, 2002
Messages
78
Hi,

Trying to get the row number of the current date on all worksheets of a workbook

The sheet2.Range("q1").Value is just the current date =Today()

Sub DATEFIND()
Dim lastrow As Integer
Dim ZeroLast As Date
Dim WS As Worksheet

ZeroLast = Sheet2.Range("q1").Value

For Each WS In Sheets

With WS
lastrow = WS.Cells.Find(ZeroLast, searchorder:=xlByRows, searchdirection:=xlPrevious).Row

WS.Range("C1").Value = lastrow
WS.Range("e1").Value = WS.Name

End With
Next WS
End Sub

The error lies in the ws.cells but I don't know how to get past this.

Please help if you can.

Paul
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try:
Code:
Sub DATEFIND()
    Application.ScreenUpdating = False
    Dim lastrow As Long
    Dim ZeroLast As String
    Dim WS As Worksheet
    ZeroLast = Sheets(2).Range("Q1")
    For Each WS In Sheets
        lastrow = WS.UsedRange.Find(CDate(ZeroLast), LookIn:=xlFormulas, lookat:=xlWhole).Row
        WS.Range("C1").Value = lastrow
        WS.Range("E1").Value = WS.Name
    Next WS
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for the reply,

I still get an error on this line of code

lastrow = WS.UsedRange.Find(CDate(ZeroLast), LookIn:=xlFormulas, lookat:=xlWhole).Row

Ideas?

I have the code in Module 1. Does that make any difference?

Paul
 
Upvote 0
I tried the macro on some dummy data and it worked properly. Are the cells which contain the dates formatted as "Date"? It is always easier to help and test possible solutions if we could work with your actual file. Perhaps you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Include a detailed explanation of what you would like to do referring to specific cells and worksheets. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Try:
Code:
Sub DATEFIND()
    Application.ScreenUpdating = False
    Dim lastrow As Long
    Dim ZeroLast As String
    Dim WS As Worksheet
    ZeroLast = Sheets("Sheet2").Range("Q1")
    For Each WS In Sheets
        lastrow = WS.UsedRange.Find(CDate(ZeroLast), LookIn:=xlFormulas, lookat:=xlWhole).Row
        WS.Range("C1").Value = lastrow
        WS.Range("E1").Value = WS.Name
    Next WS
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
That worked!! Thank you!

Was the problem in the way ZeroLast was defined?

Paul
 
Upvote 0
You are very welcome. :) The problem was the Sheet name: Sheets("Sheet2")
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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