×

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 WeekdayName Function

Excel VBA WeekdayName Function: The WeekdayName function in VBA returns a string containing the weekday name, for the specified integer representation of a weekday. Syntax WeekdayName (Weekday, [Abbreviate], [FirstDayOfWeek]) Parameter Weekday (required) – This parameter represents...

2 minutes read.

Excel VBA StrComp Function

VBA StrComp Function: The StrComp function in VBA compares two strings and returns an integer value displaying the result of the comparison. Syntax StrComp (String1, String2, [Compare]) Parameter String1 (required)- This parameter represents the first string to...

2 minutes read.

Excel VBA GoTo Statement

GoTo Statement he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends...

2 minutes read.

Scope in Visual Basics

Definition of Scope The scope of any programming language implies the area of code where the variables will be identified, accessed, and used. Every variable has a scope associated with it. The scope of...

4 minutes read.

UserForm and its Properties Excel VBA

UserForm and its Properties Userform has certain properties that can be viewed as category wise (based on appearance, behavior, font) or in an alphabetic manner. The property window is used to set or...

11 minutes read.

Excel VBA CInt Function

VBA CInt Function: The VBA Cint function converts the specified expression into an Integer. Syntax Cint (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to an Integer wherein...

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 Subscript out of Range

What is Subscript out of Range? The VBA Subscript out of Range error (which is also called as Run-Time Error 9) mostly triggers when the user selects any cell, sheet, or workbook which does...

5 minutes read.

Excel VBA For Each Loop

A For Each loop executes a statement or a group of statements for each element in an array or collection. It repeats the statement/condition/code for each element in a collection. For Each Loops loop through every...

4 minutes read.

Excel VBA Val Function

VBA Val Function: The Val function in VBA converts the given string into a numeric value. This function ignores spaces and continues to read the characters after space(s). It stops...

1 minute read.

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to...

4 minutes 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 Type Mismatch Error

What is a Type Mismatch Error? VBA Type Mismatch Error is a run time error in excel, which often occurs when the data types contained in a VBA code are not matched...

3 minutes read.

Excel VBA CByte Function

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

1 minute read.

Excel Objects in VBA

What are Excel Objects? The Excel objects belong to the entities that make up an Excel Workbook, Worksheets, Columns, Rows, Cell Ranges, etc. Each object in Excel has loads of Properties that are...

6 minutes read.

VBA Find Function

VBA Find Function The Excel VBA FIND function finds any information in your Excel. It can be used on a Range object on the worksheet. It works the same, unlike the Excel Find &...

6 minutes read.

Excel VBA StrConv Function

The StrConv function in VBA converts a string into a specified format. Syntax StrConv (String, Conversion, [LocaleID]) Parameter String (required) – This parameter represents the string to be converted. Conversion (required) – This parameter specifies the type of conversion. It can...

1 minute 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 MessageBox

Message Box The MsgBox in Excel VBA is a dialog box used to inform the users of your program by showing a custom message or get some necessary inputs such as Yes/No or...

4 minutes read.

Excel VBA Timer Function

Excel VBA Timer Function: The Timer function in VBA returns a Single data type, evaluating the number of seconds that have elapsed since midnight of the current day. Syntax Timer () Parameter NA Return This...

1 minute read.