×

VBA runtime error 1004

What is 1004 error? 

VBA 1004 Error, also known as object-defined or application-defined, is a runtime error in VBA, usually, if the specified range does not exist in the worksheet or if the Application or the object if not appropriately defined. Error 1004 can cause a severe problem and even can crash your VBA macro code, or in some cases, it freezes the system for a while. This error is encountered by all Ms Excel versions, unlike Excel 2007, 2010, 2013, 2016, and even 2019.

VBA 1004 Error is an error we encounter even if there is some corruption in the Mas Excel desktop icon or any conflict in the file concerning other programs while opening the VBA Excel file. Sometimes when you copy the filtered data and paste it to another worksheet of Ms Office Excel, the 1004 Error is thrown.

Why Error 1004 occurs?

Errors are part and parcel of any programming language. They are usually unintentional but creates loads of troubles. Runtime errors can occur anywhere in your code, even if you are a pro coder. In VBA, the Runtime Error 1004 encounters due to several reasons. Few of the common reason for the occurrence of Runtime Error 1004 are explained below:

  1. Run-time error ‘1004’: Method ‘Range’ of object ‘_ Global’ failed
VBA runtime error 1004

In Excel, we have a fixed number of rows and columns. This error occurs if the user refers to an invalid range that is out of the Excel’s list. Because it revolves around range or objects, so it displays the error message that the “Range” of object’ _ Global’ method has failed.

Example: Demonstrating the VBA code where the range variable does not exist in our worksheet.

Code:             

Sub RunTimeError1004_Example()
  'We have used an invalid headings range
  Range("Headings").Select 'throw an Error 1004
End Sub
VBA runtime error 1004

Press the F5 key to run the code. When you run the above code, you will get the following output.

Output

VBA runtime error 1004
  • Run-time error ‘1004’: That Name is already taken. Try a different One.
VBA runtime error 1004

Many times in confusion, we use the same name, which is already given to another Excel sheet in the same workbook. When the compiler reads the code and tries to refer to the sheet, but the same name creates ambiguity. Thus, it throws a runtime Error 1004, stating that the name is already taken, try with a different name.

For example, in the below code in our Excel workbook, we have three worksheets named as ‘Sheet1’, ‘Sheet2’, and ‘Sheet3’. Now, if we try to rename our worksheet ‘Sheet1’ with ‘Sheet2’ and run the code, the VBA compiler will throw a run time error.

Example: Demonstrating the VBA code where we assign an existing name to our worksheet.

Code:

Sub RunTimeError1004_Example()
 'We have replaced the worksheet  Sheet1's name
 'So there now there will be two worksheets named with 'Sheet2'
 Worksheets("Sheet1").Name = "Sheet2" 'throw an Error 1004
End Sub
VBA runtime error 1004

Press the F5 key to run the code. When you run the above code, you will get the following output.

Output

VBA runtime error 1004
  • Run-time error ‘1004’: Select method of Range class failed
VBA runtime error 1004

Sometimes this error occurs if we specify the range in some another worksheet. But we have not activated the same worksheet whose range we have selected. This error would directly mention that VBA could not access the chosen range property of Range class.

Example: Demonstrating the VBA code where the range variable does not exist in our worksheet.

Code:

Sub RunTimeError1004_Example()
  'we are using the Range property for 'Sheet4' worksheet
  'whereas our activated worksheet us Sheet2
  Worksheets("Sheet4").Range("B1:B5").Select
End Sub
VBA runtime error 1004

Press the F5 key to run the code. When you will run the above code, you will get the following output.

Output

VBA runtime error 1004
  • Run-time error ‘1004’- Method ‘Open’ of object ‘Workbooks’ failed or Microsoft Excel cannot access the file.
VBA runtime error 1004

VBA code sometimes throws the Error ‘1004’ if we try to open a workbook that is already open, or another program is already using the same file which the user is referring to. 

Example: Demonstrate the VBA code where the specified file is already opened or has the same name as the other workbook which has already been opened. 

Code:

Sub RunTimeError1004_Example()
   Dim wrkBok As Workbook
   Set wrkBok = Workbooks.Open("\\FileName123.xls", ReadOnly:=False, CorruptLoad:=xlExtractData)
End Sub
VBA runtime error 1004

Press the F5 key to run the code. When you run the above code, you will get the following output.

Output

VBA runtime error 1004
  • Run-time error ‘1004’: Sorry, we couldn’t find file_name. Is it possible it was moved, renamed, or deleted?
VBA runtime error 1004

When the user tries to open an Excel worksheet or refer to a directory that does not exist because the file has been moved, deleted, or renamed from the specified path, in that case, VBA throws an Error 1004, stating that the compiler could not find the specified file.   Sometimes, this error could also be displayed if you have misspelled the path or have typed an incorrect file name.

For example: In the below code, we have specified the file address in the path variable. But we know that the defined file path does not exist as there is some typing error.

Example: Demonstrating the VBA code where the specified path does not exist in our worksheet.

Code:

Sub RunTimeError1004_Example()
    'defining a path variable
    Dim path As String
    'assigning the path to variable wherein
    'the file does not exist it the folder
    path = "E:\Excel Files\VBAExcel\Error.xlsx"
    'VBA will throw a runtime Error 1004 method mentioning
    'Sorry, we couldn’t Find.
    Workbooks.Open Filename:= path
End Sub
VBA runtime error 1004

Press the F5 key to run the code. When you run the above code, you will get the following output.

Output

VBA runtime error 1004
  • Run-Time error ‘1004’: Activate method of Range class failed.

This error occurs when we activate the range cells without activating the Excel sheet. Thus, the activate method for range class fails as the compiler can not look for the worksheet.

For example, In the below code, we have activated the Range method. But it will throw an error as we have not specified the worksheet.

Example: Demonstrating the VBA code where the worksheet is not activated though we have accessed the Range activate method.

Code:  

Sub RunTimeError1004_Example()
  'Activating the range method without activating a worksheet.
  Worksheets("Sheet4").Range("B1:B5").Activate 'throw an error
End Sub
VBA runtime error 1004

Press the F5 key to run the code. When you run the above code, you will get the following output.

Output

VBA runtime error 1004

How to Fix the Error 1004?

  1. The range object should be valid. The row and column counting should range from 1 to 104856, and columns range from A to XFD.  
  2. Always have a note regarding your exiting Sheet name, and whenever you want to rename a worksheet, make sure it does not get repeated.
  3. Never type the path or file address directly. Always try to copy it and paste the source file.
  4. Before activating the range class, always measure that you have activated the Active worksheet prior.
  5. Sometimes there are some issues with you with the .xlsx file, or because of corrupted file also this error is thrown. In those cases, the professional recommendation solution is Ms Excel Repair Tool, which is extremely helpful to repair .xls and .xlsx file. It is a unique repairing tool which helps to fix multiple corrupted files. Ms Excel Repair Tool is capable enough to fix and repair charts, cell comments, workbook, worksheet features at once. One of the reasons for recommending this tool is its ease and supportive property, which can run both on Mac as well as Windows operation System.

Related Topics

Excel VBA Minute Function

The Minute function in VBA returns the minute component for the specified time. Syntax Minute (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the minute component for the specified time. Example 1 Sub MinteFunction_Example1() ...

1 minute read.

Excel VBA Array

Introduction to VBA Array An array is a type of variable that holds more than one piece of data. In VBA, you can refer to a specific variable (element) of an array by using...

5 minutes read.

Excel VBA CBool Function

VBA CBool Function: The CBool function in VBA calculates an expression and returns the result as a Boolean data type. Syntax CBool (Expression) Parameter Expression (required) – This parameter represents the expression that that you want...

1 minute read.

Excel VBA Format Function

VBA Format Function The format function in VBA applies a specified format to an expression and returns the result as a string. Syntax Format (Expression, [Format], [FirstDayOfWeek] , [FirstWeekOfYear] ) Parameter Expression (required)- This parameter represents the expression that you want to format. Format...

3 minutes read.

Miscellaneous Exercise of conditional statements and Loop

We have the already worked with syntax and examples of conditional statements and loops in the previous tutorials. In this tutorial, we will learn how to work with both together.  We will explain...

3 minutes read.

Excel VBA Weekday Function

Excel VBA Weekday Function: The TimeValue function in VBA returns an integer (1 to 7), signifying the day of the week for the specified date. Syntax Weekday (Date, [FirstDayOfWeek]) Parameter Date (required) – This parameter...

2 minutes read.

Excel VBA InputBox

Input Box The InputBox function in VBA is used to prompt the users to enter values. The user can click either the OK button or can choose the CANCEL button. If the user clicks...

3 minutes read.

Excel VBA CDec Function

VBA CDec Function: The CDec function in VBA converts an expression into a Decimal data type. Syntax CDec (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to a...

1 minute read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute read.

Excel VBA IsArray Function

VBA IsArray Function: The IsArray function in VBA returns a Boolean, showing whether the given variable is an Array or not. Syntax IsArray (VarName) Parameter VarName (required)- This parameter represents the variable that you want...

1 minute read.

VBA TimeSerial Function

The TimeSerial function in VBA returns a Time for the specified hour, minute, and second. Syntax TimeSerial (Hour, Minute, Second) Parameter Hour (required) – This parameter represents an integer (0 to 23), signifying the hour of the time. Minute...

2 minutes read.

VBA ActiveSheet

What is VBA Activesheet Property? The active sheet means the current worksheet which you are working on and viewing. The ActiveSheet object signifies the worksheet tab that is selected before running the VBA...

5 minutes read.

Excel VBA Cos Function

VBA Cos Function: The Cos function in VBA returns the cosine value for a supplied angle. Syntax Cos (Number) Parameter Number (required) –This parameter represents the number that you want the absolute value of. Return This function...

1 minute read.

Excel VBA AutoFilter

One of the reasons for Excel VBA’s popularity is its capability to filter and analyze data from huge database with the help of a method known as AutoFilter. This method permits a...

4 minutes read.

Procedures in VBA

Procedures in VBA A procedure is a block of statements or units of computer code that performs some action. It is enclosed with a declaration statement, and its primary purpose is to carry out...

3 minutes read.

VBA Cell

What is VBA cell? Cells is one of the elements (workbook, worksheet, range) in Excel VBA, which refers to cells of the Excel worksheet.  In VBA, the cell is also a property...

5 minutes read.

Excel VBA Round Function

VBA Round Function: The Round function in VBA rounds a number to a specified number of decimal places and returns the number. Syntax Round (Number, [NumDigitsAfterDecimal]) Parameter Number (required) –This parameter represents a numeric value one wants...

1 minute read.

Excel VBA: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

Introduction to Visual Basic Editor Window

How to enable the Developer Ribbon Tab? In order to work with VBA, users need to make a small change in Excel to display a new tab (Developer) at the top of the...

5 minutes read.

Excel VBA Error Handling

What is Errors and Types of Error? Errors are conditions that resist the flow of the program or enables a problem while running any programming. There are three types of errors in VBA...

5 minutes read.