[Fixed!] Run Time Error 1004: Select Method of Range Class FailedĀ 

In this article, we are going to learn about Run Time Error 1004: Select Method of Range Class Failed. This is one of the most common errors that users encounter while applying VBA codes. This happens when users run a code on a sheet that is not active. This interrupts the smooth operation of the Excel VBA. So, by learning the reason and possible solution to this error, users can avoid it and also resolve it accordingly.

The below video illustrates the error clearly. As our expected ā€œEmployeeā€ sheet is inactive, we get this error after running the code.


How to Launch VBA Macro Editor in Excel

In order to run any VBA code, we first need to write or edit the code in the VBA Macro Editor.

Before that, we need to enable the Developer tab, which remains hidden by default. The Developer tab allows users to access the VBA Macro Editor. Do the following tasks.

  • Go to Developer Tab >> Visual Basic.
  • This will open the Visual Basic window.

Opening Visual Basic Window

  • Select Insert >> Module in the macro editor.

Inserting VBA Module

  • As a result, an empty module will appear on the screen where you can write the code. However, use the Run icon or F5 key to run the code.

VBA Module


What Exactly Is Run Time Error 1004?

Run Time Error 1004 occurs in VBA (Visual Basic for Applications) when an attempt is made to select a range of cells that does not exist or is invalid. This error typically occurs when the VBA code tries to select a range that is not available, such as a cell that has been deleted, a range that has been shifted due to the insertion or deletion of cells, or the sheet that the code is trying to work on is not active. Typically, there are six types of Run Time Error 1004. They are:

  • VBA Run Time Error 1004: That Name is already taken. Try a different One- This happens when you try to rename a Worksheet with a name that is already taken by another Worksheet of the Workbook.
  • VBA Run Time Error 1004: Method ā€œRangeā€ of objectā€™ _ Globalā€™ failed- When you try to access a Named Range via VBA code but you somehow misspell the name of the Named Range then this error will show up.
  • VBA Run Time Error 1004: Select Method of Range class failed- It occurs when users try to select a range from a Sheet that is not active or previously selected.
  • VBA Runtime Error 1004 method open of object workbooks failed- occurs when the code attempts to open a workbook using the Workbooks.Open method and is unable to do so. This error can occur for a number of reasons, including:
    1. The workbook is already opened in another instance of Excel or by another user.
    2. The user does not have permission to access the file.
    3. The file is corrupted or damaged.
  • VBA Runtime Error 1004 method Sorry We couldnā€™t Find:- This usually happens when we try to open a Workbook and the specified file path or name is incorrect or does not exist.
  • VBA Runtime Error 1004: Application-Defined or Object-Defined Error when Selecting Range- This occurs when we try to select an object that does not exist.

Run Time Error 1004 Select Method of Range Class Failed: Possible Reason with Solutions

Today we will discuss the reason and then possible solutions to Run Time Error 1004: Select Method of Range Class Failed. This happens when a user tries to select a range from an inactive Sheet. The solution to that is very simple. The user just has to select or activate the Sheet before selecting the range.


Reason: Running Code While Worksheet Is Inactive

As the image below suggests, we are trying to select a range from the ā€œEmployeeā€ Sheet which is not active, from the ā€œSalesā€ Sheet, which is the active sheet. This results in an error for the VBA code.

Code Showing Error due to Inactive Sheet

If we debug the code, we will find that the Worksheets(ā€œEmployeeā€).Range(ā€œB5:B12ā€).Select line is the source of error. Because the ā€œEmployeeā€ Sheet is inactive. So the code cannot select the range from that Sheet.

Debugging VBA Code


Solution 1: Activating Sheet with Worksheet.Activate Method

In order to resolve the error, we need to activate the Sheet before selecting any range from it. So, we will make a modification to our current code. We will activate the Sheet first by using the Worksheet.Activate method and then select the range from the Sheet. Here is the corrected code.

Sub DotActivateMethod()
Worksheets("Employee").Activate
Worksheets("Employee").Range("B5:B12").Select
End Sub

Corrected Code with Activate Method to Resolve Run Time Error 1004

The Worksheet.Activate method makes the current sheet or any specific sheet (if we assign any sheet) an active sheet. In this code, the Worksheets(“Employee”).Activate line activates the ā€œEmployeeā€ Sheet before selecting the B5:B12 range from that Sheet.

As seen from the image below, the code works perfectly and selects the desired range.

Proper Execution of the Corrected Code

Read More: How to Fix Excel Runtime Error 13 Type Mismatch in VBA


Solution 2: Selecting Sheet with Worksheet.Select Method before Selecting Range

This is another way to fix the error. Here, we will select the Sheet with the Workhseet.Select method before selecting the range from that Sheet. Here is the modified code.

Sub DotSelectMethod()
Worksheets("Employee").Select
Worksheets("Employee").Range("B5:B12").Select
End Sub

Corrected Code with Select Method

The Workhseet.Select method allows us to select a specific worksheet in an Excel workbook. If we assign the Worksheet.Select method with any specific worksheet, the worksheet becomes the active sheet. Consequently, any commands will be applied to the worksheet. In the above code, The Worksheets(“Employee”).Select line simply selects the ā€œEmployeeā€ Sheet before selecting the range from that Sheet.

As the image below illustrates, now the code works perfectly and does not show any errors.

Properly Executed Code


How to Fix Run Time Error 1004: PasteSpecial Method of Range Class Failed Error in Excel

This is another common type of Run Time Error 1004.Ā  This usually happens when users fail to copy a range from a Sheet and paste it by using the PasteSpecial method using VBA code. Here, we are trying to paste some values in the B4:B12 range of the ā€œSalesā€ Sheet. However, if we run the code, the code shows an error, as seen in the image below.

Code Show Error in PasteSpecial Method

Now, if we debug the code, we will see that the code did not copy anything from any Sheet. So, the PasteSpecial method could not work properly.

Debugging VBA Code

So, we will make some changes to that code. We will copy some values from a range first and then paste them into our desired Sheet using the following code.

Sub PasteSpecialMethodError()
Worksheets("Employee").Range("B4:B12").Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub

Corrected Code that Copies Values from Another Sheet

Here, the Worksheets(“Employee”).Range(“B4:B12”).Copy line copies the values from the B4:B12 range of the ā€œEmployeeā€ Sheet. Besides, Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False – this line pastes only the values of the copied range in the cells taken from the user selection.

As seen from the image below, the code works perfectly and the copied range is pasted properly into the selected range.

Code Running without any Error

Read More: [Fixed!] Runtime Error 438 in Excel VBA


How to Fix VBA Runtime Error 1004: Application-Defined or Object-Defined Error When Selecting Range

In this instance, we will talk about the VBA Runtime Error 1004: Application-Defined or Object-Defined Error when selecting range. This happens when a user tries to select an object that does not exist or is invalid. As the below image shows, when we try to copy a range from a Sheet and paste it into another Sheet the VBA code shows an error. Because the Named Range we are trying to copy does not exist.

Code Showing Error due to Invalid Named Range

If we debug the code, we can clearly see that the ā€œEmployeeNameā€ Named Range does not exist and that is why the VBA editor is showing an error in that line.

Debugging VBA Code

In order to resolve the issue, we need to create a Named Range called ā€œEmployeeNameā€. Follow the steps below to do so,

  • Go to Formulas >> Define Name.
  • A prompt will appear on the screen.

Opening Define Name Tab

  • In the prompt, first type ā€œEmployeeNameā€ inside the Name box.
  • Next, select B4:B12 as the range for this Named Range.
  • Finally, click OK.
  • As a result, the B4:B12 range of the ā€œEmployeeā€ Sheet will be termed as ā€œEmployeeNameā€.

Setting Up Named Range

Now, if we run the code, it will show no errors.

Code Running without any Error


Download Practice Workbook

You can download the practice book here.


Conclusion

In this article, we have talked about Run Time Error 1004: Select Method of Range Class Failed. We tried to find out the reason behind the error and gave some possible solutions. This article will allow users to familiarize themselves with the Run Time Error 1004, which is one of the most commonly encountered errors in VBA. It will also help them to find a solution to this error and make their code more effective. Also, if you want to see more Excel content like this, please visit our website, Exceldemy, and unlock a great resource for Excel-related content.


Related Articles:

Get FREE Advanced Excel Exercises with Solutions!
Adnan Masruf
Adnan Masruf

Adnan Masruf, holding a BSc in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, plays a pivotal role as an Excel & VBA Content Developer at ExcelDemy. His deep passion for research and innovation seamlessly aligns with his dedication to Excel. In this capacity, Masruf not only skillfully addresses challenging issues but also exhibits enthusiasm and expertise in gracefully navigating intricate situations, underscoring his steadfast commitment to consistently delivering exceptional content. His interests... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo