简体   繁体   English

计算 Excel 表中填充了 VBA 数据的行

[英]Counting Excel Table Rows that are populated with data on VBA

So im currently working a table in excel that I have named Table1 with three columns (Column 1, Column 2 and Column 3).所以我目前在 excel 中工作一个表,我用三列(第 1 列、第 2 列和第 3 列)命名了 Table1。 Ive been trying to count the used rows or populated rows inside the table using VBA but have had no luck.我一直在尝试使用 VBA 计算表内已使用的行或填充的行,但没有运气。

Example 1:示例 1:

UsedRows= Sheets ("Sheet1").ListObjects.("Table1[#Column 1]").UsedRange.ListRows.Count

Example 2 (This One Returns only all available rows)示例 2(此仅返回所有可用行)

UsedRows= Sheets ("Sheet1").ListObjects.("Table1[#Column 1]").ListRows.Count

I either want the populated or unpolulated row amount.我要么想要填充的行数,要么想要未填充的行数。 Either of the two will work just fine.两者中的任何一个都可以正常工作。 Remember this is a Table so End(xlUp) and End(xlDown) work a little bit different.请记住,这是一个表格,因此 End(xlUp) 和 End(xlDown) 的工作方式略有不同。 Ive tried those too but I still get either the total rows available or the cells that are modified which is way more than what I have available.我也尝试过这些,但我仍然得到可用的总行数或修改的单元格,这比我可用的多得多。

Thanks for the help in adavanced whoever posts.感谢任何发帖者的帮助。

Sounds like you can use CountA , like this perhaps:听起来您可以使用CountA ,可能像这样:

Dim myColumn As ListColumn
Set myColumn = Sheets("Sheet1").ListObjects("Table1").ListColumns("Column 1")

Dim UsedRows As Long
UsedRows = Application.CountA(myColumn.DataBodyRange)
Debug.Print UsedRows

If you don't have blank cells in other rows.如果您在其他行中没有空白单元格。 The 3 doesn't need to be hard-coded, this is just the number of columns in your table. 3 不需要硬编码,这只是表中的列数。

Sub x()

Dim r As Range

Set r = ActiveSheet.ListObjects(1).DataBodyRange

With WorksheetFunction
    MsgBox .CountBlank(r) / 3                  'empty rows
    MsgBox (r.Rows.Count - .CountBlank(r) / 3) 'non-empty rows
End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM