[英]Counting Excel Table Rows that are populated with data on VBA
所以我目前在 excel 中工作一個表,我用三列(第 1 列、第 2 列和第 3 列)命名了 Table1。 我一直在嘗試使用 VBA 計算表內已使用的行或填充的行,但沒有運氣。
示例 1:
UsedRows= Sheets ("Sheet1").ListObjects.("Table1[#Column 1]").UsedRange.ListRows.Count
示例 2(此僅返回所有可用行)
UsedRows= Sheets ("Sheet1").ListObjects.("Table1[#Column 1]").ListRows.Count
我要么想要填充的行數,要么想要未填充的行數。 兩者中的任何一個都可以正常工作。 請記住,這是一個表格,因此 End(xlUp) 和 End(xlDown) 的工作方式略有不同。 我也嘗試過這些,但我仍然得到可用的總行數或修改的單元格,這比我可用的多得多。
感謝任何發帖者的幫助。
聽起來您可以使用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
如果您在其他行中沒有空白單元格。 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.