简体   繁体   中英

VBA excell Runtime error 424 object not found

I am running following macro to combine the column data from multiple sheets into one single sheet (into one column) in same workbook. Nmae of Workbook- ThisWorkbooks

Public Sub Test()

 Dim lRow As Long
 Dim sh As Worksheet
 Dim shArc As Worksheet

 Set shArc = ThisWorkbooks.Worksheets("Archive")
 For Each sh In ThisWorkbooks.Worksheets
     Select Case sh.Name
         Case Is <> "Archive"
             lRow = shArc.Range("A" & Rows.Count).End(xlUp).Row
             sh.Range("A1:A1000").Copy _
                 Destination:=shArc.Range("A" & lRow + 1)
     End Select
 Next

 Set shArc = Nothing
 Set sh = Nothing

 End Sub

can you tell me what must be wrong here.

It's ThisWorkbook , not ThisWorkbooks .

Drop the s in the end and you should be good to go.

Edit: I see you renamed your ThisWorkbook module to be called ThisWorkbooks? If that's the case - the above should run just fine, unless you don't have the worksheet with tab name "Archive" in the book.

try the code below , adding Option Explicit at the top of your module would have helped you detect that spelling error)

Option Explicit

' rest of your code, bla bla bla

' your loop (I like using the `With` inside)
For Each sh In ThisWorkbook.Worksheets
    With sh
        Select Case .Name
            Case Is <> "Archive"
                lRow = shArc.Range("A" & shArc.Rows.Count).End(xlUp).Row
                .Range("A1:A1000").Copy Destination:=shArc.Range("A" & lRow + 1)
        End Select
    End With
Next

The code below worked. I renamed my Excel file to "Workbook" and applied the logic below.

Public Sub Test()
     Dim lRow As Long
     Dim sh As Worksheet
     Dim shArc As Worksheet
     Set shArc = ThisWorkbook.Worksheets("Archive")
     For Each sh In ThisWorkbook.Worksheets
         Select Case sh.Name
             Case Is <> "Archive"
                 lRow = shArc.Range("A" & Rows.Count).End(xlUp).Row
                 sh.Range("A1:A2000").Copy _
                     Destination:=shArc.Range("A" & lRow + 1)
         End Select
     Next
     Set shArc = Nothing
     Set sh = Nothing
 End Sub

If you see, even I am trying to find answer to one question as to how did this work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM