0
    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

因“类型不匹配”而失败。摔了什么?选择对象手册在哪里?

4

1 回答 1

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
于 2013-02-19T09:53:02.333 回答