Excel VBA loop module sheet out of range?

2019-09-16 20:04发布

I am encountering a runtime error 1004 "Select method of Range class failed" on a script I have running within a module. The script is tied to a button on a different sheet. When run, the script is meant to look at the N column of the sheet "projects overview," find any rows containing the value "Complete - Design" and copies the values of that row to a temporary holding row on sheet3. From that point, the row is added to a third and final sheet (sheet9) where it's inserted into a running list of other rows that have been copied. The original row on the "projects overview" sheet is then deleted.

I get the error when the script reaches the Sheet3.Range("A200:Q200").Select part of the code. I feel like it has something to do with activating the sheet from within this module because the rest of the script is enveloped in a with statement that targets the projects overview sheet, but I don't know how to resolve that conflict. Any help would be appreciated. Thanks!

Sub CompleteJob()

Dim Firstrow As Long
    Dim lastRow As Long
    Dim LrowProjectsOverview As Long

    With Sheets("Projects Overview")
        .Select

        Firstrow = .UsedRange.Cells(1).Row
        lastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
        For LrowProjectsOverview = lastRow To Firstrow Step -1
            With .Cells(LrowProjectsOverview, "N")
                If Not IsError(.Value) Then
                    If .Value = "Complete - Design" Then .EntireRow.Copy

                    Sheet3.Range("A200:Q200").Select

                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False


                        If Sheet9.Range("B2").Value = "" Then
                            Sheet9.Range("A2:Q2").Value = Sheet3.Range("A200:Q200").Value
                            Sheet3.Range("A200:Q200").ClearContents

                            Else

                            Sheet9.Range("B2").EntireRow.Insert
                            Sheet9.Range("A2:Q2").Value = Sheet3.Range("A200:Q200").Value
                            Sheet3.Range("A200:Q200").ClearContents
                            Sheet9.Range("B2:Q2").Interior.Color = xlNone
                            Sheet9.Range("B2:Q2").Font.Bold = False
                            Sheet9.Range("B2:Q2").Font.Color = vbBlack
                            Sheet9.Range("B2:Q2").RowHeight = 14.25

                        End If

                        If Sheet9.Range("B2").Value = "" Then
                           Sheet9.Range("B2").EntireRow.Delete

                        End If

                    If .Value = "Complete - Design" Then .EntireRow.Delete

                End If
            End With
        Next LrowProjectsOverview
    End With

End Sub

0条回答
登录 后发表回答