Code runs from Macro but not ActiveX Button

domtrump

Board Regular
Joined
Apr 1, 2010
Messages
245
I'm trying to run some very simple code to clear out a table on one tab, then filter a table on another tab and copy the filtered data back to the table I just cleared. This all works wonderfully if I simply execute the macro. However, if I then paste the code into the Private Sub for an ActiveX button control, it fails with message "Runtime error '1004' Method 'Range' of object '_Worksheet' failed. it does so at the "Range("Table13").Select" line.

I an SURE there a much more elegant way to do this and I'm happy to see the suggestions. But could someone explain to me what is happening? How could something so simple not work just because it is being called from a button? I'd really like to understand this. Thanks!

Code:
' Clear the Additional Tabs
    Sheets("RptSheet").Select
    Range("Table13").Select
    Selection.ClearContents


' Filter Copy to Tab
    Sheets("Query").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=14, Criteria1:= _
        "="
    Range("Table1").Select
    Selection.Copy
    
    Sheets("RptSheet").Select
    Range("A2").Select
    ActiveSheet.Paste
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try fully qualifying the objects:

Code:
Private Sub CommandButton1_Click()
    Sheets("RptSheet").Range("Table13").ClearContents
    Sheets("Query").ListObjects("Table1").Range.AutoFilter Field:=2, Criteria1:="="
    Sheets("Query").Range("Table1").Copy Sheets("RptSheet").Range("a2")
End Sub
 
Upvote 0

Forum statistics

Threads
1,202,963
Messages
6,052,827
Members
444,602
Latest member
Cookaa

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