您好我对
提出的问题提出了类似的问题Move from one cell to another and count the number of rows in between two data
在我的例子中,我想计算A列中非空单元格(包括原始数据行本身)之间的行数,并将计数放在B列中
我的数据从A1开始,随着单元格中的空白向下移动,直到下一个数据行。
示例:
A1 1 B1 3
A2 B2 4
A3 B3 2
A4 2 B4 3
A5
A6
A7
A8 3
A9
A10 4
A11
A12
我试图对发布的原始解决方案进行修改,但我似乎无法得到最后一行的计数。
非常感谢任何帮助。
答案 0 :(得分:1)
不确定我是否能得到你的要求,但尝试这样的事情:
Private Sub CountSkips()
Dim lStart As Long, lEnd As Long
Dim rData As Range, rNext As Range
Dim vData As Variant
Set rData = ActiveSheet.Range("A1" _
, ActiveSheet.Cells(Application.Rows.Count, 1).End(xlUp))
vData = rData.Resize(rData.Rows.Count + 1).Value2
Set rNext = rData.Resize(1)
Do While rNext.Row <> Application.Rows.Count
lStart = rNext.Row
Set rNext = rNext.End(xlDown)
If LenB(vData(lStart + 1, 1)) = 0 Then
lEnd = rNext.Row
rNext.Offset(lStart - lEnd, 1) = lEnd - lStart
End If
Loop
End Sub
更新
另一种方法,从你的评论中听起来就像是你想要空白后的项目编号。
Private Sub CountSkips()
Dim lStart As Long, lEnd As Long, lVeryLastRow As Long
Dim rData As Range, rNext As Range
Dim vData As Variant
lVeryLastRow = Application.Rows.Count
Set rData = ActiveSheet.Range("A1" _
, ActiveSheet.Cells(lVeryLastRow, 1).End(xlUp))
vData = rData.Resize(rData.Rows.Count + 1).Value2
Set rNext = rData.Resize(1)
Do While rNext.Row <> Application.Rows.Count
lStart = rNext.Row
Set rNext = rNext.End(xlDown)
If LenB(vData(lStart + 1, 1)) = 0 And rNext.Row <> lVeryLastRow Then
lEnd = rNext.Row
rNext.Offset(, 1) = lEnd - lStart
End If
Loop
End Sub
答案 1 :(得分:0)
wsworkcenter - &GT;工作簿对象名称
WsWorkCenter.Activate
'counting non empty rows , will store count in the variable mlProjectCount
Cells(2, 1).Select
Selection.End(xlDown).Select
mlProjectCount = ActiveCell.Row