Loop through each sheet to find last row

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
ok...I'm stump.

I'm trying to loop through each sheet with the following code.

Sheet 1, the last row is 213.
Sheet 2, the last row 7.

The code doesn't seem to recognize the last row for each sheet. How do I get the code to recognize the last row for each sheet.

VBA Code:
For Each Ws In Sheets
        If Ws.Name <> "Sheet3" Then
            With Range("A7:R" & LRow)
                .SpecialCells(xlConstants).Font.Color = RGB(242, 242, 242)
                .Borders(xlEdgeLeft).Color = RGB(242, 242, 242)
                .Borders(xlEdgeRight).Color = RGB(242, 242, 242)
                .Borders(xlInsideVertical).Color = RGB(242, 242, 242)
                If Not IsLoaded("frmData") Then .Borders(xlEdgeBottom).Color = RGB(242, 242, 242)
            End With
            
            If ActiveCell.Row > 6 And Not IsEmpty(ActiveCell) Then
                With ActiveCell.Resize(, 18)
                    .Font.Color = 0
                    .Borders(xlEdgeLeft).Color = 0
                    .Borders(xlEdgeRight).Color = 0
                    .Borders(xlInsideVertical).Color = 0
                End With
            End If
            Rng.SpecialCells(xlConstants).Font.Color = RGB(14, 33, 56)
        End If
    Next
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
A few things. Looping through your sheets doesn't actually select/activate it, unless you tell it to.
And if each sheet has a different last row, then LastRow must be calculated for each sheet.

Assuming you can use column A to find the last row, maybe this will give you what you need:
VBA Code:
For Each ws In Sheets
        If ws.Name <> "Sheet3" Then
            With ws
                LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
                With .Range("A7:R" & LRow)
                    .SpecialCells(xlConstants).Font.Color = RGB(242, 242, 242)
                    .Borders(xlEdgeLeft).Color = RGB(242, 242, 242)
                    .Borders(xlEdgeRight).Color = RGB(242, 242, 242)
                    .Borders(xlInsideVertical).Color = RGB(242, 242, 242)
                    If Not IsLoaded("frmData") Then .Borders(xlEdgeBottom).Color = RGB(242, 242, 242)
                End With
            End With

             ...
As for the second block, I recommend avoid using "ActiveCell", especially if you are looping through different sheets, as it will only apply to the sheet you started on, unless you activate a different sheet. If that is something that you want to apply to the sheet you start on only, then move that above the other "With" code.
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,593
Members
449,038
Latest member
Arbind kumar

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