尝试运行会生成错误91的VBA代码

时间:2018-10-23 07:46:23

标签: excel vba

我正在使用下面的代码根据另一个单元格的值复制相应单元格的值,但是我却收到了错误91 。你能看看我做错了吗?在

上出错
Dim ws As Worksheet, Snags As Worksheet
Dim lr As Long, lrSnags As Long, i As Long
Application.ScreenUpdating = False
For Each ws In Worksheets
    If ws.Name <> "Snags" Then
        lr = ws.Range("A" & Rows.Count).End(xlUp).Row

        For i = 2 To lr
            lrSnags = Snags.Range("A" & Rows.Count).End(xlUp).Row + 1
            If ws.Range("B") = "Fail" Then
                ws.Range("A" & i).Copy
                Snags.Range("A" & lrSnags).PasteSpecial xlPasteValues
            End If
        Next i
    End If
Next ws
Application.CutCopyMode = False
Application.ScreenUpdating = True
MsgBox "Complete"

End Sub

在下面一行出现错误

lrSnags = Snags.Range("A" & Rows.Count).End(xlUp).Row + 1

1 个答案:

答案 0 :(得分:1)

我想您没有定义名为“ Snags”的工作表

您需要在定义Snags后对其进行设置: 对于前者:

   Set Snags = Worksheets("Snags")

或者,您可以只更改行而无需将其定义为:

lrSnags = Sheets("Snags").Range("A" & Rows.Count).End(xlUp).Row + 1