Code Error when trying to copy a table

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
I am getting a run-time error '91': Object variable or With block variable not set

on this code - line "Sheets("PQ AssocCost").ListObjects("PQ_AssocCost").DataBodyRange.Copy"

could this be bacuase the table that its trying to copy is empty? This may be the case from time to time. If that is the reason, whats the work-around? is there an If statement I can add (if the error is because the table is empty)?

Code:
Sub CreateStep5()
'Associated Costs Step C
'Copy PQ AssocCost Table and Paste in Step5

Application.ScreenUpdating = False

Sheets("PQ AssocCost").ListObjects("PQ_AssocCost").DataBodyRange.Copy

    Sheets("Step 5").Range("B16").Select
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    
'Copy Selected into Previously Selected so User can see what was Prev Selected

    Range("Step_5_Table[Select]").Select
    Selection.Copy

    Range("Step_5_Table[Previously_Selected]").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
          Application.CutCopyMode = False
        
    Columns("C:W").EntireColumn.AutoFit

    Sheets("Step 5").Range("B16").Select
    
Application.ScreenUpdating = True


End Sub

Thanks for the help and training! Teach a person to fish!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You can check if the table's ListRows.Count property is greater than 0 before attempting the copy.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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