Loop Thru Worksheets / MsgBox Rows disconnect

MikeL

Active Member
Joined
Mar 17, 2002
Messages
492
Office Version
  1. 365
Platform
  1. Windows
Hi,

There are 3 worksheets named A, B and C. I want to loop thru each WS and MsgBox Count of Rows and WS Name. Weird that I keep getting same count of rows from Worksheet A in MsgBox even though the other MsgBox pops up with the 3 different sheet names.
What needs to change in code?


--------------------------------
Sub ABC()

"Declare
Dim Current As Worksheet
Dim lrow As Long


' Loop thru Worksheets A B and C and MsgBox number of rows and then MsgBox Name of Worksheet

For Each Current In Worksheets

lrow = Cells(Rows.Count, "A").End(xlUp).Row
MsgBox (Str(lrow))


lrow = 0
MsgBox Current.Name

Next

MsgBox ("complete")


End Sub
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try this

Code:
lSub ABC()

'Declare
Dim Current As Worksheet
Dim lrow As Long


' Loop thru Worksheets A B and C and MsgBox number of rows and then MsgBox Name of Worksheet

For Each Current In Worksheets

lrow = Current.Cells(Rows.Count, "A").End(xlUp).Row
MsgBox Current.Name & " Has " & lrow & " rows"

Next

MsgBox ("complete")


End Sub
 
Upvote 0
Try this:
Code:
Sub Sheet_Names_And_Rows()
'Modified  2/17/2019  7:48:06 PM  EST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
For i = 1 To Sheets.Count
    Lastrow = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row
    MsgBox "Sheet Named" & vbNewLine & Sheets(i).Name & vbNewLine & "Has" & vbNewLine & Lastrow & vbNewLine & "Rows"
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
That worked. Thanks so much!

Can you tell why Current needed to be in front of Cells ? It seems if it was not , the last row was only on Worksheet A.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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