作者:帕格迪奥 | 来源:互联网 | 2023-06-24 09:47
我在VBA上很弱。
我试图在图片中找到表1和表2的行数。
![从指定单元格向上计数行 从指定单元格向上计数行](https://img.php1.cn/3cd4a/1eebe/cd5/43a754c811e7ec5c.webp)
我尝试在以下VBA代码
中从单元格
postList cache
向上(向后)计数行
A12
但是它似乎不起作用。
LastRow=sheet("Sheet1").Range("A12").end(xlUp).row
将对最多Cells(Rows.Count,1).End(xlUp).Row
行进行计数;因此不起作用。
您是否有更好的主意/代码来统计表1和表2?
谢谢!
使用Range.Areas
后,尝试使用SpecialCells
。遵循以下原则:
Sub Test()
Dim lr As Long
Dim rng As Range
With ThisWorkbook.Sheets("Sheet1")
'Get a full range of where your tables sit.
lr = .Cells(.Rows.Count,1).End(xlUp).Row
Set rng = .Range("A1:A" & lr).SpecialCells(xlCellTypeConstants)
'Loop through all different tables
For Each Area In rng.Areas
Debug.Print Area.Cells(1,1).Value & " has " & Area.Rows.Count - 1 & " rows of data."
Next Area
End With
End Sub
,
Sub FFF()
Dim rng,cell
Set cell = Cells(Rows.Count,1).End(xlUp) '//Start cell
Do
Set rng = cell.CurrentRegion
MsgBox "The count of cells in table " & rng.Address & " is: " & rng.Cells.Count
Set cell = rng(1).End(xlUp)
Loop Until cell.Row = 1
End Sub