Copy data to an empty table

SalimJ

New Member
Joined
Jul 8, 2013
Messages
22
Hi,

Could anyone help me with the following code. It works when Table2 already contains data, but I have runtime error 91 when Table2 is blank. Why?

Code:
Sub Integration()
    Dim tbl1 As Range
    Dim tbl2 As Range


    Application.ScreenUpdating = False


    Set tbl1 = Sheet1.ListObjects("Table1").DataBodyRange
    Set tbl2 = Sheet2.ListObjects("Table2").DataBodyRange


    If tbl2 Is Nothing Then
        tbl1.Copy tbl2.Offset(0)
    Else
        tbl1.Copy tbl2.Offset(tbl2.Rows.Count)
    End If
    
    Application.CutCopyMode = False
    
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
because when there is no data DataBodyRange is nothing, so tbl2 remains nothing, so eventually you remain with no destination defined to paste the data.
So you have to set it directly to the table range and skip the header row:
Code:
[B]If tbl2 is nothing Then 
set tbl2 = Sheet2.ListObjects("Table2").Range.Resize(1).Offset(1)
.....
[/B]
or something similar.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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