Macro to find last instance of today's date

maps5

New Member
Joined
Oct 15, 2009
Messages
23
Hello.

In my workbook, column A contains dates, which can be repeated.
The table is not sorted (and can't be) on column A.

I need a macro that will find the last instance of today's date, and select the cell (or table row) immediately under.


I haven't been able to find a solution for this, as all I could find only works if there is only 1 occurance of today's date (as in calendars).

Hope someone can help me on the way here.

Thank you in advance.
maps
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Change date to the one you need
Code:
Sub FindLastDate()
    Dim dtSearch As Date
    Dim cell As Range
    dtSearch = DateTime.DateSerial(2018, 5, 6)
    Set cell = Columns("A:A").Find(What:=dtSearch, SearchDirection:=xlPrevious, LookAt:=xlWhole)
    If Not cell Is Nothing Then
        cell.Offset(1).EntireRow.Select
    End If
End Sub
 
Upvote 0
Change date to the one you need
Code:
Sub FindLastDate()
    Dim dtSearch As Date
    Dim cell As Range
    dtSearch = DateTime.DateSerial(2018, 5, 6)
    Set cell = Columns("A:A").Find(What:=dtSearch, SearchDirection:=xlPrevious, LookAt:=xlWhole)
    If Not cell Is Nothing Then
        cell.Offset(1).EntireRow.Select
    End If
End Sub


Thank you, Sektor!

This worked perfectly.

I've addapted it to always look for today's date.

Since I tried it on a table without a record/row with today's date, no row was selected... (as expected)
But this brings me to my next request.

Can I put this code in a loop, so that if today's date isn't found, it'll look for yesterday, and select the row below the last instance of yesterday's date, if found...
The loop should repeat until it finds a date within the past 7 days... if nothing is found, it should stop searching, meaning that nothing will be selected...

Thank you once again for the help.
 
Upvote 0
How about
Code:
Sub FindLastDate()
    Dim dtSearch As Date
    Dim cell As Range
    Dim i As Long
    For i = 0 To 6
      dtSearch = Date - i
      Set cell = Columns("A:A").Find(What:=dtSearch, SearchDirection:=xlPrevious, LookAt:=xlWhole)
      If Not cell Is Nothing Then
          cell.Offset(1).EntireRow.Select
          Exit For
      End If
    Next i
End Sub
 
Upvote 0
How about
Code:
Sub FindLastDate()
    Dim dtSearch As Date
    Dim cell As Range
    Dim i As Long
    For i = 0 To 6
      dtSearch = Date - i
      Set cell = Columns("A:A").Find(What:=dtSearch, SearchDirection:=xlPrevious, LookAt:=xlWhole)
      If Not cell Is Nothing Then
          cell.Offset(1).EntireRow.Select
          Exit For
      End If
    Next i
End Sub


Thank you very much, Fluff!
This seems to be working wonderfully.

In your code, I don't seem to see where the date is being set to today's date...
Do you mind explaining how the code knows the first date that it needs to search for?

Thank you once again.
Regards,
maps
 
Upvote 0
The line in blue
Code:
For i = 0 To 6
      [COLOR=#0000ff]dtSearch = Date - i[/COLOR]
sets the date, so on the first loop i =0 so dtSearch will be today's date, on the second pass i=1 so date -1 will be yesterday, etc
 
Upvote 0
The line in blue
Code:
For i = 0 To 6
      [COLOR=#0000ff]dtSearch = Date - i[/COLOR]
sets the date, so on the first loop i =0 so dtSearch will be today's date, on the second pass i=1 so date -1 will be yesterday, etc

ah, yes... I got it now...

Another thing...
The code will select the entire row... how can I tweak it so only the table row is selected?

I tried the following right after the row is selected, and after the For cycle ran it's course (just below Next i), but to no avail...

Code:
Range(Selection, Selection.End(xlToLeft)).Select
 
Upvote 0
Try
Code:
cell.offset(1).resize(,[COLOR=#ff0000]10[/COLOR]).select
where the value in red needs to be changed to the number of columns.
 
Upvote 0
Try
Code:
cell.offset(1).resize(,[COLOR=#ff0000]10[/COLOR]).select
where the value in red needs to be changed to the number of columns.


Hello Fluff.

The code worked as expected...
But for my case it only almost worked... :)

The problem is that the date I'm searching for is in column K, which means that this line of code is selecting a given row from column K and 10 columns forward...
for instance, the selection is resized from the entire row to become K19:T19...
And, based on my trials, I can't input a negative number as an argument for the resize...

How can I offset the selection to start in column A and x columns forward (10 in your example), instead of starting in column K, which is the column where it looks for the date?

Thank you in advance.



EDIT:

I seem to have fixed it with the following line...

Code:
Selection.Offset(0, -10).Select

Is there a more 'elegant' way of doing this?

Thanks.
 
Last edited:
Upvote 0
There are various ways of doing it & here are 3 options
Code:
cell.Offset(, -10).Resize(, 10).Select
Range("A" & cell.Row).Resize(, 10).Select
Intersect(cell.EntireRow, Range("A:J")).Select
All give the same result, which one you use is mainly personal preference.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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