Sameer sand
Sameer sand

Reputation: 1

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.

Upvotes: 0

Views: 587

Answers (3)

Sameer sand
Sameer sand

Reputation: 1

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?

Upvotes: 0

Shai Rado
Shai Rado

Reputation: 33692

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

Upvotes: 0

Rik Sportel
Rik Sportel

Reputation: 2679

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.

Upvotes: 1

Related Questions