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
 
@wndncg - this uses the same terminology used in the other parts of your code.
It uses Match which is much more forgiving of date format mismatches.

Just replace your whole LAST_SPECIFIC sub with this one.

VBA Code:
Sub LAST_SPECIFIC()
    Dim wsData As Worksheet, wsTemp As Worksheet
    Dim Rng As Range
    Dim LR As Long, DateAgency_LR As Long
    Dim rngMth As Range, rngAgency As Range
    Dim strFormula
    
    ' Set worksheet variables
    Set wsData = Sheets("TEMPLATE")
    Set wsTemp = Sheets("FIN")
    
    With wsData
        LR = .Cells(Rows.Count, "B").End(xlUp).Row
        Set Rng = .Range("A" & LR + 1)
        Set rngMth = .Range("A1:A" & LR)
        Set rngAgency = .Range("B1:B" & LR)
        ' Get Last Row no for specified Date & Agency combination
        strFormula = "iferror(Match(2, 1 / ((" & rngMth.Address & " = " & .Range("E1").Address & ")*" _
                                        & "(" & rngAgency.Address & " = " & .Range("A1").Address & "))),0)"
        DateAgency_LR = Evaluate(strFormula)
        
        If DateAgency_LR = 0 Then
            MsgBox "Date / Agency combination not found: " & Format(.Range("E1"), .Range("E1").NumberFormatLocal) & " / " & .Range("A1").Value
            Exit Sub
        End If
        .Range("A" & DateAgency_LR).Select
    End With
    
    Application.Wait (Now + TimeValue("00:00:01"))
    wsTemp.Activate
    Call INSERT_ROW_SPECIFIC
    'ActiveCell.EntireRow.Insert
End Sub
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
And I tested it on his actual sheet. There is no number formatting issues. It works on his sheet as it did on my hand-made test sheet.
thanks for the effort Godbless in my end its not really working.
 
Upvote 0
@wndncg - this uses the same terminology used in the other parts of your code.
It uses Match which is much more forgiving of date format mismatches.

Just replace your whole LAST_SPECIFIC sub with this one.

VBA Code:
Sub LAST_SPECIFIC()
    Dim wsData As Worksheet, wsTemp As Worksheet
    Dim Rng As Range
    Dim LR As Long, DateAgency_LR As Long
    Dim rngMth As Range, rngAgency As Range
    Dim strFormula
  
    ' Set worksheet variables
    Set wsData = Sheets("TEMPLATE")
    Set wsTemp = Sheets("FIN")
  
    With wsData
        LR = .Cells(Rows.Count, "B").End(xlUp).Row
        Set Rng = .Range("A" & LR + 1)
        Set rngMth = .Range("A1:A" & LR)
        Set rngAgency = .Range("B1:B" & LR)
        ' Get Last Row no for specified Date & Agency combination
        strFormula = "iferror(Match(2, 1 / ((" & rngMth.Address & " = " & .Range("E1").Address & ")*" _
                                        & "(" & rngAgency.Address & " = " & .Range("A1").Address & "))),0)"
        DateAgency_LR = Evaluate(strFormula)
      
        If DateAgency_LR = 0 Then
            MsgBox "Date / Agency combination not found: " & Format(.Range("E1"), .Range("E1").NumberFormatLocal) & " / " & .Range("A1").Value
            Exit Sub
        End If
        .Range("A" & DateAgency_LR).Select
    End With
  
    Application.Wait (Now + TimeValue("00:00:01"))
    wsTemp.Activate
    Call INSERT_ROW_SPECIFIC
    'ActiveCell.EntireRow.Insert
End Sub

Did you try my post #41 code ?

working please close.
 
Upvote 0
Thanks for letting us know. Glad we could help.

PS: now that it is working consider doing a replace all on wsTemp to perhaps wsFIN.
Since one of the sheets is called Template and it is a bit confusing the way it is currently named.
 
Upvote 0
As far as I know, Range.Find doesn't search in the backwards direction.
Yes it does, just change the "SearchDirection" argument, to xlPrevious rather than xlNext.
 
Last edited:
Upvote 0
Thanks for letting us know. Glad we could help.

PS: now that it is working consider doing a replace all on wsTemp to perhaps wsFIN.
Since one of the sheets is called Template and it is a bit confusing the way it is currently named.
how to edit this that i will find in FIN?
 
Upvote 0
Assuming you have not yet done the "Replace" I suggested and we are talking about switching the same piece of code to the FIN sheet. Try this:
(I wouldn't normally use Select and Activate but the rest of your code seems to rely on it)

VBA Code:
Sub LAST_SPECIFIC_FindLast_Rvrs()
    Dim wsData As Worksheet, wsTemp As Worksheet
    Dim Rng As Range
    Dim LR As Long, DateAgency_LR As Long
    Dim rngMth As Range, rngAgency As Range
    Dim strFormula
    
    ' Set worksheet variables
    Set wsData = Sheets("TEMPLATE")
    Set wsTemp = Sheets("FIN")
    
    With wsTemp
        LR = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rngMth = .Range("A1:A" & LR)
        Set rngAgency = .Range("B1:B" & LR)

        ' Get Last Row no for specified Date & Agency combination
        .Activate
        strFormula = "iferror(Match(2, 1 / ((" & rngMth.Address & " = " & wsData.Range("E1").Address(External:=True) & ")*" _
                                        & "(" & rngAgency.Address & " = " & wsData.Range("A1").Address(External:=True) & "))),0)"
        DateAgency_LR = Evaluate(strFormula)
    End With
    
    With wsData
        If DateAgency_LR = 0 Then
            MsgBox "Date / Agency combination not found: " & Format(.Range("E1"), .Range("E1").NumberFormatLocal) & " / " & .Range("A1").Value
            Exit Sub
        End If
    End With
    
    wsTemp.Range("A" & DateAgency_LR).Select

    Application.Wait (Now + TimeValue("00:00:01"))
    'wsTemp.Activate            ' No longer required performed earlier before cell select
    Call INSERT_ROW_SPECIFIC
    'ActiveCell.EntireRow.Insert
End Sub
 
Upvote 0
Solution
Assuming you have not yet done the "Replace" I suggested and we are talking about switching the same piece of code to the FIN sheet. Try this:
(I wouldn't normally use Select and Activate but the rest of your code seems to rely on it)

VBA Code:
Sub LAST_SPECIFIC_FindLast_Rvrs()
    Dim wsData As Worksheet, wsTemp As Worksheet
    Dim Rng As Range
    Dim LR As Long, DateAgency_LR As Long
    Dim rngMth As Range, rngAgency As Range
    Dim strFormula
   
    ' Set worksheet variables
    Set wsData = Sheets("TEMPLATE")
    Set wsTemp = Sheets("FIN")
   
    With wsTemp
        LR = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rngMth = .Range("A1:A" & LR)
        Set rngAgency = .Range("B1:B" & LR)

        ' Get Last Row no for specified Date & Agency combination
        .Activate
        strFormula = "iferror(Match(2, 1 / ((" & rngMth.Address & " = " & wsData.Range("E1").Address(External:=True) & ")*" _
                                        & "(" & rngAgency.Address & " = " & wsData.Range("A1").Address(External:=True) & "))),0)"
        DateAgency_LR = Evaluate(strFormula)
    End With
   
    With wsData
        If DateAgency_LR = 0 Then
            MsgBox "Date / Agency combination not found: " & Format(.Range("E1"), .Range("E1").NumberFormatLocal) & " / " & .Range("A1").Value
            Exit Sub
        End If
    End With
   
    wsTemp.Range("A" & DateAgency_LR).Select

    Application.Wait (Now + TimeValue("00:00:01"))
    'wsTemp.Activate            ' No longer required performed earlier before cell select
    Call INSERT_ROW_SPECIFIC
    'ActiveCell.EntireRow.Insert
End Sub
Worked, please do close.
 
Upvote 0
Assuming you have not yet done the "Replace" I suggested and we are talking about switching the same piece of code to the FIN sheet. Try this:
(I wouldn't normally use Select and Activate but the rest of your code seems to rely on it)

VBA Code:
Sub LAST_SPECIFIC_FindLast_Rvrs()
    Dim wsData As Worksheet, wsTemp As Worksheet
    Dim Rng As Range
    Dim LR As Long, DateAgency_LR As Long
    Dim rngMth As Range, rngAgency As Range
    Dim strFormula
   
    ' Set worksheet variables
    Set wsData = Sheets("TEMPLATE")
    Set wsTemp = Sheets("FIN")
   
    With wsTemp
        LR = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rngMth = .Range("A1:A" & LR)
        Set rngAgency = .Range("B1:B" & LR)

        ' Get Last Row no for specified Date & Agency combination
        .Activate
        strFormula = "iferror(Match(2, 1 / ((" & rngMth.Address & " = " & wsData.Range("E1").Address(External:=True) & ")*" _
                                        & "(" & rngAgency.Address & " = " & wsData.Range("A1").Address(External:=True) & "))),0)"
        DateAgency_LR = Evaluate(strFormula)
    End With
   
    With wsData
        If DateAgency_LR = 0 Then
            MsgBox "Date / Agency combination not found: " & Format(.Range("E1"), .Range("E1").NumberFormatLocal) & " / " & .Range("A1").Value
            Exit Sub
        End If
    End With
   
    wsTemp.Range("A" & DateAgency_LR).Select

    Application.Wait (Now + TimeValue("00:00:01"))
    'wsTemp.Activate            ' No longer required performed earlier before cell select
    Call INSERT_ROW_SPECIFIC
    'ActiveCell.EntireRow.Insert
End Sub
How can change this A1 to F1?
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
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