我是VBA的新手,并尝试构建代码以对一行中具有相同值的单元格进行计数(理想情况下,我也希望它对单元格进行计数,并在单元格的值与上一个值不同时开始计算(我想建立趋势,向我显示项目处于最后状态的几周)。因此,我构建了以下内容:
Sub Loop2()
Dim rw As Long
Dim cl As Long
Dim tb1 As ListObject
Dim tRows As Long
Dim tCols As Long
Dim n As Integer
n = 1
Set tb1 = ActiveSheet.ListObjects("Table1")
With tb1.DataBodyRange
tRows = .Rows.Count
tCols = .Columns.Count
End With
MsgBox tRows & " " & tCols
For rw = 2 To rw = tRows
For cl = 2 To cl = tCols
If Cells(rw, cl).Value = Cells(rw, cl + 1).Value Then
n = n + 1
ElseIf Cells(rw, cl).Value <> Cells(rw, cl + 1).Value Then
n = 1
End If
Next cl
Cells(rw, tCols + 1).Value = n
Next rw
End Sub
它可以编译,但是没有给我结果(我只有一个msg框)。循环不起作用。能否给我提示我在这里做错了什么?
我怀疑一切:D