For Each r In Selection.Rows
For Each c In r
sum = sum + 1
Next c
Next r
计算行数但不计算单元格
For Each r In Selection.Rows
For Each c In r
msgbox "val = " & c.Value
Next c
Next r
因“类型不匹配”而失败。跌倒了什么? Selection对象手册在哪里?
答案 0 :(得分:3)
由于您没有明确表示要进行迭代,For Each c in r
将返回多个单元格范围。
如果您在Sum = ...
处休息并在观察窗口中查看c.Address
,则可以看到这一点。
要修复,请尝试此
Dim c As Range, r As Range
Dim Sum As Long
For Each r In Selection.Rows
For Each c In r.Cells
Sum = Sum + 1
Next c
Next r
甚至是这个
Dim c As Range
Dim Sum As Long
For Each c In Selection.Cells
Sum = Sum + 1
Next c