Finding closest date

Exceldud

New Member
Joined
Jan 30, 2022
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
I have a spreadsheet which has thousands of rows with new rows getting added daily. Using VBA how can I go directly to the cell in column D which has the date closest to the today's date. Basically I want to prevent the user from scrolling through thousands of records to get to the records that are closer to today's date. The vba should run at the time the file is opened and focus immediately goes to the record closer to today's date.

I had found the below code online but it does not seem to work.

Sub Today_Date()
Closest = Evaluate("=MATCH(MIN(ABS(TODAY()-D2:D10000)),ABS(TODAY()-D2:D10000),0)")
Range("D2:D10000").Cells(, Closest).Select
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this variation in the ThisWorkbook module of your workbook

I have assumed that your workbook may have more than one worksheet but the worksheet you want this to happen on is called "Sheet1"

VBA Code:
Private Sub Workbook_Open()
  Dim closest As Long
  
  Sheets("Sheet1").Activate
  closest = Evaluate("=MATCH(MIN(ABS(TODAY()-D2:D10000)),ABS(TODAY()-D2:D10000),0)")
  Range("D2:D10000").Cells(closest).Select
End Sub
 
Upvote 0
Solution
You're welcome. Thanks for the confirmation. :)
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,738
Members
449,094
Latest member
dsharae57

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