VBA code - jump to first line with specific date

DarkSmile

Board Regular
Joined
Feb 22, 2021
Messages
90
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm looking for a VBA code that does the following:

I have a cell (B2), where I wanna put in any date (see Printscreen), I would like to push a button and jump to the first row with that date.
Note, the first 4 rows are frozen, so it would be nice if it "scrolled" to the right cell (see second printscreen).

If looked on google, but it's a lot of 'today', but I need any date.

1673591698083.png


1673591880770.png



Any help would be really apricated.

Thank you !

David
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
try this.

VBA Code:
Sub sbDate()
    ActiveWindow.ScrollRow = Cells.Find(What:=Range("B2"), After:=Range("B2"), LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, MatchByte:=False, SearchFormat:=False).Row
End Sub
 
Upvote 0
Can you Try THis
VBA Code:
Sub JumpToDate()
    Dim dateToFind As Date
    Dim i As Long
    
    dateToFind = Range("B2").Value

    For i = 5 To Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(i, 1).Value = dateToFind Then
            Range(Cells(i, 1), Cells(i, 1)).Activate
            ActiveWindow.ScrollRow = i
            Exit For
        End If
    Next i
End Sub

This code will take the date entered in cell B2 and search through column A starting at row 5 (since the first 4 rows are frozen) for that date. When it finds the date, it activates the cell and uses the ActiveWindow.ScrollRow property to scroll the worksheet so that the row containing the date is at the top of the visible area. You can assign this code to a button or shape by right-clicking the button, selecting "Assign Macro", and selecting the "JumpToDate" macro.
 
Upvote 0
Solution
You can either:
1) Assign below coe to button:
VBA Code:
Sub JumpToDate()
Range("A" & WorksheetFunction.Match(Range("B2"), Columns(1), 0)).Select
End Sub

or
2) Put this code in worksheet module, to trigger any change in cell B2
(Right click on tab name, view code then paste below code)
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) <> "B2" Then Exit Sub
Range("A" & WorksheetFunction.Match(Target, Columns(1), 0)).Select
End Sub
 
Upvote 0
try this.

VBA Code:
Sub sbDate()
    ActiveWindow.ScrollRow = Cells.Find(What:=Range("B2"), After:=Range("B2"), LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, MatchByte:=False, SearchFormat:=False).Row
End Sub

Sub JumpToDate() Dim dateToFind As Date Dim i As Long dateToFind = Range("B2").Value For i = 5 To Cells(Rows.Count, 1).End(xlUp).Row If Cells(i, 1).Value = dateToFind Then Range(Cells(i, 1), Cells(i, 1)).Activate ActiveWindow.ScrollRow = i Exit For End If Next i End Sub

Both worked great, thank you !
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,734
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