×

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

VBA UBound

VBA UBound  The UBound or Upper Bound function in VBA is used to specify the length of an array and returns the highest subscript for a dimension for the specified array. It is...

3 minutes read.

Excel VBA DateSerial Function

The DateSerial function in VBA returns a Date from a supplied year, month, and day number. Syntax DateSerial (Year, Month, Day) Parameter Year (required) – This parameter represents an integer signifying the year. Month (required) – This parameter...

2 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 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.

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

VBA FormatPercent Function: The FormatPercent function in VBA is used to apply a percent format to a numeric expression, and it returns the result as a string. Syntax FormatPercent (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 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 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 VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type). Syntax CSng (Expression) Parameter Expression (required) – This parameter represents the expression that you want...

1 minute 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 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 Hour Function

The hour function in VBA returns the hour element for the specified time. Syntax Hour (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the hour element for the specified time. Example 1 Sub HourFunction_Example1() ...

1 minute read.

Excel VBA DateDiff Function

The DateDiff function in VBA returns a Long data value representing the number of intervals between two specified dates/times where the type of interval is supplied by the user. Syntax DateDiff (Interval, Date1, Date2, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required)...

2 minutes read.

Four VBA Clear methods

Four VBA Clear methods In Microsoft Excel, many times, a situation arises where the user wants to clear the data or any specific range of data. What if the user automates this task with...

6 minutes read.

VBA Screen Updating

What is VBA Screen Updating property? Screen Updating is a VBA property which is used to display the output generation while running the code. If this property is enabled, we could see...

6 minutes read.

Excel VBA Right Function

VBA Right Function: The Right function in VBA returns a substring from the end of the given string. Syntax Right (Str, Length) Parameter Str (required) – This parameter represents the string from which you want to...

1 minute read.

How to save Macro Workbook?

Saving Macro Workbook Default Excel File Extension The default excel file extension is “.xlsx”. But the standard file extension “.xlsx” cannot contains macros. So, the workbook contains macro when save din.xlsx file, all VBA...

1 minute read.

Excel VBA UCase Function

The UCase function in VBA converts a String into upper case text. Syntax UCase (String) Parameter String (required) – This parameter represents the string that you want to convert to upper case. Return This function returns a string...

1 minute read.

Excel VBA- Pivot Table Fields

VBA- Pivot Table Fields: The Pivot Fields collection contains all the fields from the data source, including any calculated fields. The main aspect of adding a field is its Position...

4 minutes read.

Excel VBA Chr Function

VBA Chr Function: The Chr function in VBA returns the character equivalent to a supplied character code between 0 and 255. Syntax Chr (CharCode) Parameter CharCode (required) – This parameter represents the character code for...

1 minute read.