【问题标题】:Handling `Run-time error 1004: No cells were found` when copying filtered table cells复制过滤的表格单元格时处理“运行时错误 1004:未找到单元格”
【发布时间】:2017-12-26 13:07:30
【问题描述】:

我有一个程序可以复制在 3 个循环中标识的表格的指定单元格。有时不会有任何数据,这不是问题, 但我收到了Run-time error '1004': No cells were found.,这会阻止代码进入下一个循环。
我曾尝试过On Error Resume Next,但我不会从代码中得到我的回复。也许还有更多错误,因为Resume Next,我不会看到它们。

这是我的代码:

If Sheets("hour report").Range("U1") = 1 Then
    For i = 1 To k3
        For n = 1 To k1
            Sheets("Sheet1").ListObjects("Table1").Range.AutoFilter Field:=1, _
                Criteria1:=Sheets("hour report").Range("B" & i + 1)
            Sheets("Sheet1").ListObjects("Table1").Range.AutoFilter Field:=2, _
                Criteria1:=Sheets("hour report").Cells(1, n + 2)

            On Error Resume Next
            Range("Table1[f1]").SpecialCells(xlCellTypeVisible).Copy
            Sheets("hour report").Activate
            Cells(i + 1, n + 2).Select
            ActiveSheet.Paste
        Next n
    Next i
End If

【问题讨论】:

  • 请提供一些示例数据。这将允许重现您的问题。
  • 您需要为此显示更多代码。您有我们看不到的变量,例如 k1 和 k3。
  • 通过换行符和代码格式改进了格式。文字略有改动。语言的使用仍有一些改进。

标签: excel runtime-error vba


【解决方案1】:

On Error Resume Next 快到了。这将阻止错误发生,但正如你所说,它会进一步下降。

如果您从SpecialCells 行中删除错误检查,请恢复它,然后检查SpecialCells 是否返回任何内容:

Sub Test()

    Dim rFiltered As Range

    On Error Resume Next
    Set rFiltered = Range("Table1[F1]").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If Not rFiltered Is Nothing Then
        rFiltered.Copy
        'Rest of code for when a range is returned.
    End If

End Sub

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多