Stacking multiple tables - Runtime 91 object variable

ShelleyBelly

New Member
Joined
Mar 2, 2011
Messages
44
Hi All,

I've the below code which does the trick but I get a runtime error 91 on the last line starting with "tbl" . I've used on error resume and it will happily work, but this is ofcourse not the answer. I believe the error lies in the fact that the data body range will be empty following the first if statement, but the first line starting with "tbl" doesn't throw an error. Any help would be much appreciated

Code:
Private Sub CombineTracks()
Application.ScreenUpdating = False
    
Dim Tbl As ListObject
Dim Mastertbl As ListObject
Dim ws As Worksheet




Set ws = Limbs
Set Mastertbl = Body.ListObjects("Body")


'Delete all table rows except first row
    With Mastertbl.DataBodyRange
        If .Rows.count > 1 Then
            .Offset(1, 0).Resize(.Rows.count - 1, .Columns.count).Rows.Delete
        End If
    End With


'Loop through each table in the worksheet
    For Each Tbl In ws.ListObjects
        If Mastertbl.DataBodyRange.Rows.count = 1 Then
            Tbl.DataBodyRange.Copy destination:=Mastertbl.DataBodyRange(1, 1)
        Else
            Tbl.DataBodyRange.Copy destination:=Mastertbl.DataBodyRange(Mastertbl.ListRows.count + 1, 1)
        End If
    Next Tbl


Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Untested but this might work:

Code:
            Tbl.DataBodyRange.Copy Destination:=Mastertbl.DataBodyRange(1, 1).Offset(Mastertbl.ListRows.Count, 0)

WBD
 
Upvote 0
Go to that line in the code and press F9 to set a breakpoint. Now run the code and it should stop on that line. Press Ctrl+G to bring up the immediate window and try each of the following:

Code:
? Tbl.DataBodyRange.Address
? Mastertbl.DataBodyRange.Address
? Mastertbl.ListRows.Count

Presumably one of them will generate error 91 - which one?

WBD
 
Upvote 0
So i tapped my way through and is hung up on the last copy when it gets to a table that is empty. I've added and if line to skip empty tables
Code:
'Loop through each table in the worksheet
    For Each Tbl In ws.ListObjects
        If Not Tbl.DataBodyRange Is Nothing Then
            If Mastertbl.DataBodyRange.Rows.count = 1 Then
                Tbl.DataBodyRange.Copy destination:=Mastertbl.DataBodyRange(1, 1)
             Else
                Tbl.DataBodyRange.Copy destination:=Mastertbl.DataBodyRange(Mastertbl.ListRows.count + 1, 1)
            End If
        End If
    Next Tbl

thanks for the help in hunting the problem

Tom
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top