GO TO THE LAST DATA BASED ON 2 CELL DATA

wndncg

Board Regular
Joined
Mar 24, 2017
Messages
84
Office Version
  1. 2019
  2. 2016
  3. 2013
I have a working code just for 1 data but i need based on 2 data. ty mrexcel.

Dim Rng As Range
Set Rng = Range("A" & Rows.Count).End(xlUp).Offset(1)
Set wsCellVal = Sheets("TEMPLATE")
Set wsFIN = Sheets("FIN")
cv = wsCellVal.Range("E1")
Columns("A").Find(cv, After:=Rng, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, _
SearchFormat:=False).Select
Application.Wait (Now + TimeValue("00:00:01"))
'wsFIN.Activate
'Call INSERT_ROW_SPECIFIC
'ActiveCell.EntireRow.Insert
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Sorry for the delay. I was trying to use Application.WorksheetFunction.Match instead of Range.Find (just for fun), but it failed on me. Will get to that on my own time! (Run the top/small sub.)
VBA Code:
Sub Test__Find_Latest_Date()
Call Find_Latest_Date("TEMPLATE", Sheets("TEMPLATE").Range("E1").Value, "A:A")
End Sub
Sub Find_Latest_Date(sheetName As String, search As String, searchRangeAddress As String)

With Sheets(sheetName)
    Dim FoundCell As Range, myRange As Range, LastCell As Range
    Set myRange = .Range(searchRangeAddress)

    'By default, the search find feature will start searching from the beginning once it's at the end.
    'So to guarantee that the search starts at the beginning, make the start cell to search the last cell in the search range.
    Set LastCell = myRange.Cells(myRange.Cells.Count)
    Set FoundCell = myRange.Find(What:=search, after:=LastCell, SearchDirection:=xlNext, MatchCase:=True, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows)

    'See if anything was found
    If Not FoundCell Is Nothing Then
        Dim rowMatches As String, firstOccurrenceAddress As String
        firstOccurrenceAddress = FoundCell.Address

        'Loop until cycled through all unique finds
        Do Until FoundCell Is Nothing
            rowMatches = rowMatches & "," & FoundCell.Row
            Set FoundCell = myRange.Find(What:=search, after:=FoundCell, SearchDirection:=xlNext, MatchCase:=True, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows)
            If FoundCell.Address = firstOccurrenceAddress Then GoTo Finished
        Loop
    Else
        GoTo Nothing_Found
    End If
 
Finished:
    If Left(rowMatches, 1) = "," Then rowMatches = Right(rowMatches, Len(rowMatches) - 1)
 
    Dim s() As String
    s = Split(rowMatches, ",")
 
    Dim i As Integer
    For i = UBound(s) To 0 Step -1
        If .Cells(s(i), "B").Value = .Range("A1").Value Then
            .Range("A" & s(i) & ":" & "G" & s(i)).Select
            ActiveWindow.Zoom = True
            Exit Sub
        End If
    Next i
End With

Nothing_Found:
MsgBox "Not found", vbInformation, "Search Complete"

End Sub
 
Upvote 0
Sorry for the delay. I was trying to use Application.WorksheetFunction.Match instead of Range.Find (just for fun), but it failed on me. Will get to that on my own time! (Run the top/small sub.)
VBA Code:
Sub Test__Find_Latest_Date()
Call Find_Latest_Date("TEMPLATE", Sheets("TEMPLATE").Range("E1").Value, "A:A")
End Sub
Sub Find_Latest_Date(sheetName As String, search As String, searchRangeAddress As String)

With Sheets(sheetName)
    Dim FoundCell As Range, myRange As Range, LastCell As Range
    Set myRange = .Range(searchRangeAddress)

    'By default, the search find feature will start searching from the beginning once it's at the end.
    'So to guarantee that the search starts at the beginning, make the start cell to search the last cell in the search range.
    Set LastCell = myRange.Cells(myRange.Cells.Count)
    Set FoundCell = myRange.Find(What:=search, after:=LastCell, SearchDirection:=xlNext, MatchCase:=True, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows)

    'See if anything was found
    If Not FoundCell Is Nothing Then
        Dim rowMatches As String, firstOccurrenceAddress As String
        firstOccurrenceAddress = FoundCell.Address

        'Loop until cycled through all unique finds
        Do Until FoundCell Is Nothing
            rowMatches = rowMatches & "," & FoundCell.Row
            Set FoundCell = myRange.Find(What:=search, after:=FoundCell, SearchDirection:=xlNext, MatchCase:=True, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows)
            If FoundCell.Address = firstOccurrenceAddress Then GoTo Finished
        Loop
    Else
        GoTo Nothing_Found
    End If
 
Finished:
    If Left(rowMatches, 1) = "," Then rowMatches = Right(rowMatches, Len(rowMatches) - 1)
 
    Dim s() As String
    s = Split(rowMatches, ",")
 
    Dim i As Integer
    For i = UBound(s) To 0 Step -1
        If .Cells(s(i), "B").Value = .Range("A1").Value Then
            .Range("A" & s(i) & ":" & "G" & s(i)).Select
            ActiveWindow.Zoom = True
            Exit Sub
        End If
    Next i
End With

Nothing_Found:
MsgBox "Not found", vbInformation, "Search Complete"

End Sub
no sorry sir, i will test it right now.
 
Upvote 0
where do i put the A1 and E1?
It's already put in there.

The E1 is here:
VBA Code:
Call Find_Latest_Date("TEMPLATE", Sheets("TEMPLATE").Range("E1").Value, "A:A")

The A1 is here:
VBA Code:
        If .Cells(s(i), "B").Value = .Range("A1").Value Then
 
Upvote 0
It's already put in there.

The E1 is here:
VBA Code:
Call Find_Latest_Date("TEMPLATE", Sheets("TEMPLATE").Range("E1").Value, "A:A")

The A1 is here:
VBA Code:
        If .Cells(s(i), "B").Value = .Range("A1").Value Then
what date format should user input in E1??
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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