Run-time error '1004': Select method of Range class failed

Damian37

Active Member
Joined
Jun 9, 2014
Messages
301
Office Version
  1. 365
Hello All,
I'm not entirely sure what is going on. I used the below code successfully, but when I attempted to assign the code to a button on the spreadsheet I received the Run-time error. I'm not entirely sure why the code would suddenly stop working. The code I'm using is below:
VBA Code:
Sub Beltway()


   Dim TableSheet As Worksheet
   Dim NewWB As Workbook
 
   Set TableSheet = Worksheets("AllData")
 
   With TableSheet
 
      .ListObjects("Table6").Range.AutoFilter Field:=3, Criteria1:="Beltway"
      .ListObjects("Table6").Range.AutoFilter Field:=5, Criteria1:="Open*"
      .ListObjects("Table6").Range.AutoFilter Field:=4, Criteria1:=Array("CSC", "SX", "XM; 2.5", "XM; 2.6", "XM; 3", "XR", "XM; 1", "XM; 2", "Mall Kiosk", "XM", "Kiosk", "3.0", "XR Lite", "Legacy Pre-Paid", "Pop Up", "Pop-Up", "PR", "Sat Loc 3351"), Operator:=xlFilterValues
   
     [B] .Range("Table6[[#Headers],[Store Id]]").Select[/B]
      .Range(Selection, Selection.End(xlToRight)).Select
      .Range(Selection, Selection.End(xlDown)).Select
 
   End With
 
   Selection.Copy
   Set NewWB = Workbooks.Add
 
   With NewWB.Worksheets(1)
      .Paste
      Application.CutCopyMode = False
      .Cells.EntireColumn.AutoFit
   End With
 
   NewWB.SaveAs Filename:="C:\Users\DVelez202\Desktop\POC Validation-Request\Non-BP Beltway POC Region " & Format(Date, "mm.dd.yyyy") & ".xlsx", _
                FileFormat:=xlOpenXMLWorkbook, _
                CreateBackup:=False
             

End Sub

The line with Bold codes within my code is where I'm sent to when I click on "Debug", but as I stated before, the code was working just fine, and I didn't make any changes other than trying to assign the code to a form button within the spreadsheet I'm using. Very confused.

D.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hello,
I figured out why I was receiving the above error code, and now when I run the code manually it runs without issue. However, when I attempt to create a form button to call the sub code that I have, I receive the above error message again. I'm not sure why the code would work manually, but not work when I attempt to run it using the form button. Any ideas would be greatly appreciated. Thank you.

D.
 
Upvote 0
Is the button on the Worksheets("AllData") sheet?
 
Upvote 0
You can't use Select on a sheet other than the active sheet. The ActiveSheet is the sheet with your button when you click it .
 
Upvote 0
You can't use Select on a sheet other than the active sheet. The ActiveSheet is the sheet with your button when you click it .
Not sure I understand your response. When I run the code manually, which is to say, my Call Sub the code works just fine. I only receive this message when I attempt to use the button I created. As you can see from the above code, I'm setting the Tablesheet with the tab the data is contained in. I don't think I'm fully understanding your response. The button should only be running the the Call Sub, which should then run the code below it. Am I missing something?
 
Upvote 0
When you run the code manually is Worksheets("AllData") the Active sheet?

Select can only be used on the active sheet. If you are using Select for a sheet that is not the active sheet then it will error.

You are clicking a button on a sheet other than Worksheets("AllData") which makes the other sheet active but your code is trying to do a select on Worksheets("AllData"), which isn't the active sheet.
 
Upvote 0
There are probably better ways of doing this but try this, it removes the select statements, which @MARK858 pointed out is what is most likely causing your button to fail.
Note: If StoreID is actually the first column, you could simplify this further.

VBA Code:
Sub Beltway()
    
    Dim TableSheet As Worksheet
    Dim NewWB As Workbook
    
    Dim colStoreID As Long
    Dim colLast As Long
    Dim rngTblCopy As Range
    
    
    Set TableSheet = Worksheets("AllData")
    
    With TableSheet.ListObjects("Table6")
            .Range.AutoFilter Field:=3, Criteria1:="Beltway"
            .Range.AutoFilter Field:=5, Criteria1:="Open*"
            .Range.AutoFilter Field:=4, Criteria1:=Array("CSC", "SX", "XM; 2.5", "XM; 2.6", "XM; 3", "XR", "XM; 1", "XM; 2", "Mall Kiosk", "XM", "Kiosk", "3.0", "XR Lite", "Legacy Pre-Paid", "Pop Up", "Pop-Up", "PR", "Sat Loc 3351"), Operator:=xlFilterValues
            colStoreID = .ListColumns("Store ID").Range.Column
            colLast = .Range.Columns.Count
            Set rngTblCopy = .Range.Columns(colStoreID).Resize(, colLast - colStoreID + 1)
        
    End With
        
        rngTblCopy.Copy
        Set NewWB = Workbooks.Add
        
        With NewWB.Worksheets(1)
           .Paste
           Application.CutCopyMode = False
           .Cells.EntireColumn.AutoFit
        End With
        
        NewWB.SaveAs Filename:="C:\Users\DVelez202\Desktop\POC Validation-Request\Non-BP Beltway POC Region " & Format(Date, "mm.dd.yyyy") & ".xlsx", _
                     FileFormat:=xlOpenXMLWorkbook, _
                     CreateBackup:=False
             
End Sub
 
Upvote 0
Solution
There are probably better ways of doing this but try this, it removes the select statements, which @MARK858 pointed out is what is most likely causing your button to fail.
Note: If StoreID is actually the first column, you could simplify this further.

VBA Code:
Sub Beltway()
   
    Dim TableSheet As Worksheet
    Dim NewWB As Workbook
   
    Dim colStoreID As Long
    Dim colLast As Long
    Dim rngTblCopy As Range
   
   
    Set TableSheet = Worksheets("AllData")
   
    With TableSheet.ListObjects("Table6")
            .Range.AutoFilter Field:=3, Criteria1:="Beltway"
            .Range.AutoFilter Field:=5, Criteria1:="Open*"
            .Range.AutoFilter Field:=4, Criteria1:=Array("CSC", "SX", "XM; 2.5", "XM; 2.6", "XM; 3", "XR", "XM; 1", "XM; 2", "Mall Kiosk", "XM", "Kiosk", "3.0", "XR Lite", "Legacy Pre-Paid", "Pop Up", "Pop-Up", "PR", "Sat Loc 3351"), Operator:=xlFilterValues
            colStoreID = .ListColumns("Store ID").Range.Column
            colLast = .Range.Columns.Count
            Set rngTblCopy = .Range.Columns(colStoreID).Resize(, colLast - colStoreID + 1)
       
    End With
       
        rngTblCopy.Copy
        Set NewWB = Workbooks.Add
       
        With NewWB.Worksheets(1)
           .Paste
           Application.CutCopyMode = False
           .Cells.EntireColumn.AutoFit
        End With
       
        NewWB.SaveAs Filename:="C:\Users\DVelez202\Desktop\POC Validation-Request\Non-BP Beltway POC Region " & Format(Date, "mm.dd.yyyy") & ".xlsx", _
                     FileFormat:=xlOpenXMLWorkbook, _
                     CreateBackup:=False
            
End Sub
Thank you very much Alex! This worked like a charm. I will be sure to keep a copy of this code handy. Thank you again!

D.
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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