Run-Time Error 1004 - Select method of range class failed

2000km12

New Member
Joined
Apr 19, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello, I am extremely new to VBA. I am trying to do something very simple and I am running into this error. In my Excel Workbook, there are several sheets. The ones of interest are named "CAR Daily Report Macro" and "CAR Daily Report." I am setting up a macro to copy and paste data from another workbook (this part of the code is working) and I want it to delete the top 5 rows on the new sheet, which is where I am getting the error code. I (think) I changed the active sheet, so I do not know what is happening. here is my code:





Private Sub CAR_Daily_Report_Click()



'Settings to make Macro load faster
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False


'Create new sheet and copy information from exported sheet
Sheets.Add.Name = "CAR Daily Report"

Workbooks("02. CARs_5. All.xlsx").Worksheets("02. CARs_5. All").Range("A1:G3000").Copy _
Workbooks("Main Macro Workbook.xlsm").Worksheets("CAR Daily Report").Range("A1")

'Change the active sheet
Worksheets("CAR Daily Report").Activate


'Attempt at deleting top rows
Rows("1:5").Select
Selection.Delete Shift:=xlUp

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayStatusBar = True
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True



End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi @2000km12. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

If you are going to delete the first 5 rows, then only copy from row 6 onwards:
VBA Code:
Private Sub CAR_Daily_Report_Click()
  'Create new sheet and copy information from exported sheet
  Sheets.Add.Name = "CAR Daily Report"
 
  Workbooks("02. CARs_5. All.xlsx").Worksheets("02. CARs_5. All").Range("A6:G3000").Copy _
    ThisWorkbook.Worksheets("CAR Daily Report").Range("A1")
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
Thank you for your response! I am stupid and didnt think of that! From a learning standpoint however, why cannot I select rows 1-5 on the new worksheet after I have already transfered it over? I changed my active sheet, so I would think it would allow me to do that.

I want to know why it isn't working as this is not the end of my code. A lot more selections and deletions happen after that and I need to know how to do those since I am getting the same error code for any selecting I am doing. Thanks
 
Upvote 0
I want to know why it isn't working a
Your code as it is, works for me, no errors.

Can you tell exactly what the error message says?
At which line of the macro does it stop?

There are other factors that you have not told us:
- You have hidden sheets.
- You have the sheet protected.
- You have the book protected.
- You have hidden rows.
- Do you have formulas in the source sheet?

There are several factors that I cannot guess.

But with two books in normal conditions and the sheets in normal conditions, your macro works without problem.


You can delete the rows without selecting them, it's a best practice, for example:

Rich (BB code):
Private Sub CAR_Daily_Report_Click_v0()
  'Settings to make Macro load faster
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual
  Application.DisplayStatusBar = False
  Application.EnableEvents = False
  ActiveSheet.DisplayPageBreaks = False
 
  'Create new sheet and copy information from exported sheet
  Sheets.Add.Name = "CAR Daily Report"
 
  Workbooks("02. CARs_5. All.xlsx").Worksheets("02. CARs_5. All").Range("A1:G3000").Copy _
    ThisWorkbook.Worksheets("CAR Daily Report").Range("A1")
 
  'Change the active sheet
  Worksheets("CAR Daily Report").Rows("1:5").Delete Shift:=xlUp
 
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic
  Application.DisplayStatusBar = True
  Application.EnableEvents = True
  ActiveSheet.DisplayPageBreaks = True
End Sub
 
Upvote 0
Solution
Deleting the rows without selecting them is working now, although I would love to find out why it wasnt working. It was freezing at the "select" line of code with the error code in the screenshot below. I do not have any hidden rows, volumns, sheets, or workbooks to my knowledge. Although I do work for a defense company and the sheet might be protected without my knowledge?
1682006395890.png
 
Upvote 0
I already solved your problem in 2 ways.
I don't know why you have an error when selecting the rows, as I told you, there may be factors that I can't see, because I don't have your books.
But selecting the rows to delete them, it's a bad practice and it's done in several steps, and as I already showed you, you can solve it in a single line to delete.

I want to know why it isn't working as this is not the end of my code. A lot more selections and deletions happen after that and I need to know how to do those since I am getting the same error code for any selecting I am doing. Thanks
So if you are going to delete rows, you already saw how you should do it.
No need to select.

Try everything in other books.

That is, run the test like I do, on 2 new workbooks, and check the result of your macro.
That way you can see that the macro works in new books and the problem is in the original books.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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