0
Public rctr
Public cctr
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
    Call RCount
    Call CCount
    If Range("B" & rctr) = "" Then
    Range("A" & rctr).Select
    ActiveCell.FormulaR1C1 = "=COUNTA(R2C:R[-1]C)"
    Selection.AutoFill Destination:=Range("A" & rctr & ":" & Cells(rctr, cctr - 1).Address), Type:=xlFillDefault
    Range("B" & rctr + 1).Select
    ActiveCell.FormulaR1C1 = "=R[-1]C1-R[-1]C"
    Selection.AutoFill Destination:=Range("B" & rctr + 1 & ":" & Cells(rctr + 1, cctr - 1).Address), Type:=xlFillDefault

    Cells(1, cctr).Select
    ActiveCell.FormulaR1C1 = "TOTAL"
    Cells(2, cctr).Select
    ActiveCell.FormulaR1C1 = "COUNTA(RC2:RC[-1])"
    Selection.AutoFill Destination:=Range(Cells(2, cctr).Address & ":" & Cells(rctr - 1, cctr).Address), Type:=xlFillDefault
    Range("A1").Select
    Else
        Beep
    End If
End Sub
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+Shift+C
'
    Call RCount
    Call CCount
    If Range("B" & rctr) <> "" Then
    Range("A" & rctr - 1 & ":" & Cells(rctr, cctr).Address).Select
    Selection.ClearContents
    Range(Cells(1, cctr - 1).Address & ":" & Cells(rctr, cctr - 1).Address).Select
    Selection.ClearContents
    Range("A1").Select

    Else
        Beep
    End If

End Sub

Sub RCount()
    rctr = 1
    Do
        rctr = rctr + 1
    Loop While (Range("A" & rctr) <> "")
End Sub
Sub CCount()
    cctr = 1
    Do
        cctr = cctr + 1
    **Loop While (Cells("A" & cctr).Address <> "")** 
End Sub

我有一个运行时错误 1004 Application defined or object define error with this loop 。有人请帮帮我。我不知道该怎么办。我是 VBA 编程的新手

4

1 回答 1

1

我为您的 CCount 过程收到不同的错误,但它应该是Range,而不是Cells

Sub CCount()
    cctr = 1
    Do
        cctr = cctr + 1
    Loop While (Range("A" & cctr).Address <> "")
End Sub

然后这会产生您所说的错误,因为它将循环到列中的最后一个单元格,并且尝试移动到下一个单元格是错误的 - 没有下一个单元格。Range 总是有一个Address所以你的条件 <> "" 总是得到满足。

您的 RCount 过程也有同样的问题。

如果您想要工作表中的总行数,那么它就是Rows.Count.

于 2013-07-17T11:41:20.280 回答