每张表格的Excel行数

每张表格的Excel行数,excel,vba,Excel,Vba,我目前正在使用下面的代码计算每张工作表中的所有行数,并将其打印在工作簿的主工作表上。现在,我正在尝试打印特定工作表最后一行以及主工作表中每张工作表上使用的行数 Function Test_It() Dim printRow As Integer printRow = 2 For Each Sheet In ThisWorkbook.Sheets Range("N" & printRow).Value = "Sheet Name:"

我目前正在使用下面的代码计算每张工作表中的所有行数,并将其打印在工作簿的主工作表上。现在,我正在尝试打印特定工作表最后一行以及主工作表中每张工作表上使用的行数

    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
感谢您的帮助,提前谢谢


不起作用的代码

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

这就是目标:

主页:

第2页:

第3页:

第4页:


最后的代码我要工作了

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

现在唯一的问题是,当按钮被多次按下时,它将重新计算从14行到现在28行的行数,依此类推(取决于按钮被按下的次数)。如何解决这个问题?

一些基本的问题可能是:

Sub Test()

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
Next sht

With Sheet1
    .Range("A2").Resize(dict.Count).Value = Application.Transpose(dict.Keys)
    .Range("B2").Resize(dict.Count).Value = Application.Transpose(dict.Items)
End With

End Sub
如果要避免包含“主”工作表(以您的情况为准),请在
sht.Name
属性上执行
If
条件。

您可以使用自定义项

Public Function RowCount(Optional ShtRef As Range) As Long

    Application.Volatile

    Dim Ref As Range

    If ShtRef Is Nothing Then
        Set Ref = Application.Caller
    Else
        Set Ref = ShtRef
    End If

    With Ref
        If ShtRef Is Nothing Then
            RowCount = .End(xlUp).Row 'Last row above where formula is called from.
        Else
            RowCount = .Parent.Cells(.Parent.Rows.Count, 1).End(xlUp).Row 'All rows on referenced sheet.
        End If
    End With

End Function  
现在,公式
=RowCount()
将返回上面A列中键入公式的最后一行,而
=RowCount(Sheet2!$A$1)
将返回Sheet2上的总行数


不完美,因为它会包含您在查看其他表单时使用的
行,所以不希望从中得到可接受的答案-只是为了提供信息。

您的确切问题是什么?将以前的代码转换为新的目的时出现了什么错误/问题?如果当前未显示,请说明您尝试过的内容,并在适用的情况下给出示例。每张工作表@Cyril
LastRow=ActiveWorkbook.Worksheets.Count的行数是多少?这是您实际拥有的吗?对于工作表计数来说,这是一个容易混淆的变量名。如果
countTotal
是一个
Long
,您也不能执行
countTotal=“”
。那么请修复该命名-为了我们和您的理智:-)此代码可以工作,但它只在sheet1上打印工作表名称,我需要在所有工作表上打印它,从使用的最后一行开始再打印一行。如图中第2-4页所示,其中显示“使用的行:x”。如何添加它呢?然后您必须调整for each sht循环中的代码以包含它。现在,它向您展示了如何获取最后使用的行=)