I'm currently using the code below to count all the rows in each sheet and it prints on the main sheet of the workbook. Now, I'm trying to print the amount of rows used on each sheet in the last row of that specific sheet and on the main sheet as well.
Function Test_It()
Dim printRow As Integer
printRow = 2
For Each Sheet In ThisWorkbook.Sheets
Range("N" & printRow).Value = "Sheet Name:"
Range("O" & printRow).Value = Sheet.Name
Range("P" & printRow).Value = "Count:"
Range("Q" & printRow).Value = CountMyRows(Sheet.Name)
printRow = printRow + 1
Next Sheet
End Function
Function CountMyRows(SName As String) As Long '# where SName is the name of a sheet
Dim RowCount As Long
RowCount = ThisWorkbook.Worksheets(SName).UsedRange.Rows.Count - 1
CountMyRows = RowCount
End Function
Any help is appreciated, thanks in advance!
Code that didn't work
Sub LineCount()
Dim ws As Worksheet
Dim RowCount As Integer
Dim countTotal As Long
Dim myArray() As Variant
RowCount= ActiveWorkbook.Worksheets.Count
countTotal = RowCount
ReDim myArray(1 To RowCount)
For i = 1 To RowCount
countTotal = ""
myArray(1) = Worksheets(i).UsedRange.Rows.Count
Debug.Print myArray(1)
Range("A" & countTotal).Value = countTotal
Next
End Sub
This is the goal:
Main sheet:
Sheet 2:
Sheet 3:
Sheet 4:
Final code I got to work
Sub LineCount()
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
For Each sht In ThisWorkbook.Worksheets
dict(sht.Name) = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
sht.Range("A" & Rows.Count).End(xlUp).Offset(2, 0) = "Rows Used: " & dict(sht.Name)
Next sht
With Sheet1
.Range("A1").Resize(dict.Count).Value = Application.Transpose(dict.Keys)
.Range("B1").Resize(dict.Count).Value = Application.Transpose(dict.Items)
End With
End Sub
The only issue now is that when the button is pressed multiple times, it will recount the rows from 14 rows to now 28 rows and so on (depending on how many times the button is pressed). How could this be resolved?