该代码有时会运行,有时会在运行时通过错误消息显示

时间:2019-07-11 12:56:00

标签: excel vba

下面提到的代码有时会起作用,有时会抛出错误消息

有人可以告诉我为什么会这样吗?

Sheet1.Range("H2", Range("O" & Rows.Count).End(xlUp)).Clear

错误消息是

"Run-time error '1004':Application defined or object-defined error"

2 个答案:

答案 0 :(得分:1)

现在它应该工作了:

> test_frame %>% mutate(result=func_then_add(.$arg_1, .$arg_2, .$func))
Error in f(x) : could not find function "f"

只有第一个范围正在获取工作表引用,第二个范围和行功能却没有。

答案 1 :(得分:0)

Option Explicit

Sub test()

    Dim LastRow As Long

    'Create a with statement referring BOTH workbook & worksheet to avoid errors if you have another workbook open
    With ThisWorkbook.Worksheets("Sheet1")
        'Calculate LastRow and check if it is correct
        LastRow = .Cells(.Rows.Count, "O").End(xlUp).Row
        'Clearly show your range to be clear
        .Range("H2:O" & LastRow).Clear
    End With

End Sub