Find last row of sheet based off cell value to reference the sheet name

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
857
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a interesting dilemma where I am trying to deploy a formula that seems to only work If used range are utilized. The problem is the formula must be dynamic to reference the left most first character for the sheet name to reference but I am not sure how to replicate that to find the last row on its respective sheet?

My next item H ends on row 5,000, and the one after I ends on 10,000. I am setting this all via VBA. In previous code I do loop through the sheets setting the range as I go but I logically don't think I can reference it for this. I am only including the VBA if you think I can utilize it in any way.

COUNTIFS(INDIRECT(LEFT(A3,1)&"!$D$2:$D$2217"),A3,INDIRECT(LEFT(A3,1)&"!$Q$2#"),">1")
ListEligible
G1234567845

VBA Code:
' Run loop for range, clear, run, copy and paste into its respective sheet
  i = 0
  Do Until WsCus.Range("FILTER").Offset(i, 0) = ""
    FILTER = WsCus.Range("FILTER").Offset(i, 0)
    SheetName = WsCus.Range("FILTER").Offset(i, -1).Value 'Assuming from your screenshot it's in the column left of the filter

'apply filter to start loop and activate sheet
With WsSec
    .Range("F4") = FILTER
    .Application.Calculation = xlManual
    .Activate
End With

    Call Clear
    Call S2
  
'aftter execution copy from source and paste into relative sheet applying formulas
With WsSec
lastRow = .Cells(WsSec.rows.count, "A").End(xlUp).row 'find the maximum row
    .Range("A10:L" & lastRow).Copy
End With

On Error Resume Next
    If Not Worksheets(SheetName).Name = WsCus.Range("FILTER").Offset(i, -1).Value Then Worksheets.Add.Name = WsCus.Range("FILTER").Offset(i, -1).Value
On Error GoTo 0

With Worksheets(SheetName)
        .Range("A1").PasteSpecial Paste:=xlPasteValues
        .Application.CutCopyMode = False 'Clear the copy-paste memory
lr1 = .Cells(rows.count, "A").End(xlUp).row
        .Range("M1:Q1") = Array("Rounded 4 digit Cost", "Greater than 1yr", "Greater than 3yr", "For formula", "Same Cost Occurrence")
        .Application.Calculation = xlAutomatic
        .Range("P2:P" & lr1).NumberFormat = "General"
        .Range("M2:M" & lr1).Formula = "=Round(K2, 4)"
        .Range("N2:N" & lr1).Formula = "=if(RUN-E2>365,""YES"",""NO"")"
        .Range("O2:O" & lr1).Formula = "=if(RUN-E2>(365*3),""YES"",""NO"")"
        .Range("P2:P" & lr1).Formula = "=D2&M2"
        .Range("P2:P" & lr1).NumberFormat = "@"
        .Range("Q2").Formula2 = "=COUNTIF($P$2:$P$" & lr1 & ",$P$2:$P$" & lr1 & ")"
        '.Range("P2").Formula2 = "=COUNTIFS($D$2:$D$" & lr1 & ",$D$2:$D$" & lr1 & ",$M$2:$M$" & lr1 & ",$M$2:$M$" & lr1 & ")"
        .Range("M2:P" & lr1).Value = .Range("M2:P" & lr1).Value
        .Cells.EntireColumn.AutoFit
        .Activate
End With

ActiveWindow.ScrollRow = 1 'the row you want to scroll to
ActiveWindow.ScrollColumn = 1 'the column you want to scroll to
  
    i = i + 1
Loop

With WsSec
    .Activate
End With
Call Clear

With WsSum
    .Range("B2:B" & lr1).Formula2 = "=COUNTIFS(INDIRECT(LEFT(A2,1)&""!$D$2:$D$2217""),A2,INDIRECT(LEFT(A2,1)&""!$Q$2#""),"">1"")"
End With
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,223,879
Messages
6,175,142
Members
452,615
Latest member
bogeys2birdies

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