Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

find next

0

i have a table with hundreds of data and need to go to the next record after todays date using vba.

I know i can use ctrl/f but i need it to be by using a macro if possible please.

 am not very good at vba and need help with it please

thanks in advance

Answer
Discuss

Discussion

Hi Philip and welcome to the Forum
Please edit your original question and use the Add Files button to attach a sample Excel file to illustrate your data (we nearly always need that to make sense of your query). Doesn't need to have hundreds of rows, just a few will do. 
John_Ru (rep: 6142) Jun 23, '21 at 3:05 pm
Philip
I forgot to ask you to edit the title (of your original question) to something more meaningful, to guide other TeachExcel users. Something like "Find next date in a column using VBA" would do. 
John_Ru (rep: 6142) Jun 23, '21 at 4:48 pm
Add to Discussion

Answers

0

Philip

In the revised file attached, you'll see a green button "Find first row for today or later" whih is assigned to the code below (in Module1). Click it and you'll be moved to the first cell found with today's date (or the next date after if there isn't one).

The IF statement compares each cell  to today's date, which is given by Date in VBA (I used that rather than VBA's Range.Find method since an exact match might not be found).

I've added comments to let you know what's happening:

Sub RowAfterToday()

With ThisWorkbook.Worksheets("Sheet1") 'Do the next things with this
    LastRw = .Range("A" & Rows.Count).End(xlUp).Row ' find last row with any data in column A

    For Each Cll In .Range("A2:A" & LastRw) 'loop through column A used range
        If Cll.Value >= Date Then 'check if cell value is today or later
            Cll.Select 'if so, select then
            Exit Sub 'exit macro
        End If
    Next Cll ' if not, loop around
End With ' stop doing things under the With

End Sub
It will work if you add a few or hundreds of extra rows (since the With and line starting LastRw= work out the last populated row in column A)

Hope this works for you.

Discuss

Discussion

Hi John,

Just wondering if you have a typo in your code. Should "Cll" be "Cell"? Or is "Cll" just something I am not familiar with?

Doug
WillieD24 (rep: 557) Jun 24, '21 at 12:53 pm
Hi Doug.

Thanks for the question but no, I'm deliberately using Cll as a variable (without declaring it) to avoid confusion with the Worksheet property Cells(row, column)  in VBA but while sounding like a cell. Run the macro in the workbook and you'll find it works.
John_Ru (rep: 6142) Jun 24, '21 at 12:58 pm
Philip- did that work for you?
John_Ru (rep: 6142) Jun 28, '21 at 11:54 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login