Ошибка при выполнении макроса 1004

Four ways to fix runtime error 1004 in Excel:

Workable Solutions Step-by-step Troubleshooting
Fix 1. Delete the GWXL97.XLA Files Fix the Excel error 1004 is to find and delete the error file. Go to C:\Program Files\MS Office\Office\XLSTART…Full steps
Fix 2. Check the Trust Access to the VBA Project Object Model Enable a VBA project trust option in Excel Trust Center to fix Excel error 1004. Open a blank Excel file…Full steps
Fix 3. Create Another Excel Template Start a new Excel workbook and make sure there is only one worksheet in it. Format the workbook first…Full steps
Fix 4. Repair Corrupted Excel File Repair corrupted Excel files with a file recovery tool. EaseUS file repair tool fixes severely corrupted XLS and XLSX files and retrieves everything from Excel…Full steps

Microsoft Visual Basic for Applications (VBA) is developed to help users write programs for the Windows operating system. It runs as an internal programming language in Microsoft Office, such as Word, Excel, and PowerPoint.

Some users have reported that when running VBA in an Excel chart or trying to generate a Macro in Excel documents, an error message popped up saying: Runtime error 1004. And then they find themselves cannot access the Excel files. If you have the same encounter as these users, this post is the right place for you. You can find both the reasons and the corresponding solutions of this error code on this page.

How to Fix Excel Error 1004

Runtime Error Details

The error message contains more information than the error code 1004. Generally, follow the error code, you can see a brief description. The most repeated error messages are listed below:

  1. Runtime error 1004: Application or object-defined error.
  2. Runtime error 1004: Method Ranger of Object Worksheet failed.
  3. Runtime error 1004: Copy Method of Worksheet Class failed.

The Reason Why You See Runtime Error 1004 in Excel

If you want to know how to fix runtime error 1004 in Excel properly, you need to understand what leads to this issue. The following are the most prominent reasons.

  • Macro Name Error

The Macro you are running is copying the original worksheet to a workbook with a defined name that you did not save and close before running the Macro.

  • File Conflict

When opening the VBA Excel file, it gets conflicted with other programs.

  • Too Many Legend Entries

The Excel chart contains more legend entries than space available to display the legend entries on the chart.

  • Excel File Corruption

Your .xls files got corrupted, infected, or damaged.

Although many reasons would cause this Excel error 1004 problem, luckily, some valid methods can help users re-access the files. Let’s check them one by one.

Fix 1. Delete the GWXL97.XLA Files to Fix Runtime Error 1004 in Excel

The easiest method to fix the Excel error 1004 is to find and delete the error file.

Step 1. Go to C:\Program Files\MS Office\Office\XLSTART.

Step 2. Find GWXL97.XLA file and delete it.

Step 3. Reopen your Excel file and check if the problem is solved.

Fix 2. Check the Trust Access to the VBA Project Object Model

Another solution you can try is to enable a VBA project trust option in Excel Trust Center. Follow the detailed steps and have a try.

Step 1. Open a blank Excel file and click «Files» on the upper left.

Step 2. Click Option and go to Trust Center.

Enter Excel Option

Step 3. Find and enter the Trust Center Settings.

Enter Trust Center Settings

Step 4. Under Macro Settings, tick the option of «Trust access to the VBA project object model.»

Trust Access to the VBA Project

Now you can check your Excel file.

Fix 3. Create Another Excel Template to Fix Runtime Error 1004 in Excel

This method could be a little bit complicated, but it’s useful and worth trying.

Step 1. Please start a new Excel workbook and make sure there is only one worksheet in it.

Step 2. Format the workbook first and then put the data you need onto it.

Step 3. Tap File > Save As, first enter the file name, and click the unfold arrow in Save as Type column.

Excel Save As

Excel 2003: Choose Excel 97-2003 Template.

Excel 2007 or Later: Choose Excel Template.

Choose the Right Template

Step 4. Click «Save» to confirm.

Now you can insert it programmatically by using the following code: Add Type:=path\filename. The file name is the one you set when you create the new Excel template.

Fix 4. Repair Corrupted Excel Files Due to Error 1004 

If all the above solutions can’t help you out, then there is one possibility that the Excel file you want to open is damaged. To fix a damaged Excel file, you can rely on file repair software. EaseUS Fixo Document Repair is a great choice.

With this tool, click the «Repair» button and wait for it to fix all the corrupted documents for you.

  • Repair various corrupted files, including repairing Word, Excel, and PDF document 
  • Fix unreadable contents in Word efficiently
  • Repair corrupted PDF files, extract the text, comments, labels, graphics, etc. 
  • Compatible with Microsoft Office 2019, 2016, 2013, 2010, & previous versions.

Download the software and follow the detailed steps below to fix corrupted Excel files.

Step 1. Download and launch Fixo on your PC or laptop. Choose «File Repair» to repair corrupted Office documents, including Word, Excel, PDF, and others. Click «Add Files» to select corrupted documents.

add files to repair documents with Fixo

Step 2. To repair multiple files at once, click the «Repair All» button. If you want to repair a single document, move your pointer to the target file and choose «Repair». You can also preview the documents by clicking the eye icon.

select documents to repair

Step 3. Click «Save» to save a selected document. Select «Save All» to save all the repaired files. Choose «View Repaired» to locate the repaired folder.

save repaired documents in Fixo

The Bottom Line

After reading, you must have a thorough understanding of how to fix Runtime error 1004. If you can make sure that the Excel file you want to open is valid, then the first three methods would help you out.

Once you got a damaged Excel file, a professional file recovery tool is a wiser choice. EaseUS Fixo is highly recommended by many users & IT professionals to help you repair Word, Excel, PowerPoint, and PDF files. 

Return to VBA Code Examples

In this Article

  • VBA Error 1004 – Object does not exist
  • VBA Error 1004 – Name Already Taken
  • VBA Error 1004 – Incorrectly Referencing an Object
  • VBA Error 1004 – Object Not Found

This tutorial will explain the VBA Error 1004- Application-Defined or Object-Defined Error.

VBA run-time error 1004 is known as an Application-Defined or Object-Defined error which occurs while the code is running. Making coding errors (See our Error Handling Guide) is an unavoidable aspect learning VBA but knowing why an error occurs helps you to avoid making errors in future coding.

VBA Error 1004 – Object does not exist

If we are referring to an object in our code such as a Range Name that has not been defined, then this error can occur as the VBA code will be unable to find the name.

Sub CopyRange()
  Dim CopyFrom As Range
  Dim CopyTo As Range
  Set CopyFrom = Sheets(1).Range("CopyFrom")
  Set CopyTo = Sheets(1).Range("CopyTo")
  CopyFrom.Copy
CopyTo.PasteSpecial xlPasteValues
End Sub

The example above will copy the values from the named range “CopyFrom” to the named range “CopyTo” – on condition of course that these are existing named ranges!  If they do not exist, then the Error 1004 will display.

VBA Error1004 1

The simplest way to avoid this error in the example above is to create the range names in the Excel workbook, or refer to the range in the traditional row and column format eg: Range(“A1:A10”).

VBA Error 1004 – Name Already Taken

The error can also occur if you are trying to rename an object to an object that already exists – for example if we are trying to rename Sheet1 but the name you are giving the sheet is already the name of another sheet.

Sub NameWorksheet()
  ActiveSheet.Name = "Sheet2"
End Sub

If we already have a Sheet2, then the error will occur.

VBA Error1004 2

VBA Error 1004 – Incorrectly Referencing an Object

The error can also occur when you have incorrectly referenced an object in your code. For example:

Sub CopyRange()
  Dim CopyFrom As Range
  Dim CopyTo As Range
  Set CopyFrom = Range("A1:A10")
  Set CopyTo = Range("C1:C10")
  Range(CopyFrom).Copy
  Range(CopyTo).PasteSpecial xlPasteValues
End Sub

This will once again give us the Error 10004

VBA Error1004 1

Correct the code, and the error will no longer be shown.

Sub CopyRange()
  Dim CopyFrom As Range
  Dim CopyTo As Range
  Set CopyFrom = Range("A1:A10")
  Set CopyTo = Range("C1:C10")
  CopyFrom.Copy
  CopyTo.PasteSpecial xlPasteValues
End Sub

VBA Error 1004 – Object Not Found

This error can also occur when we are trying to open a workbook and the workbook is not found – the workbook in this instance being the object that is not found.

Sub OpenFile()
 Dim wb As Workbook
 Set wb = Workbooks.Open("C:\Data\TestFile.xlsx")
End Sub

Although the message will be different in the error box, the error is still 1004.

VBA Error 1004 FileNotFound

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
vba save as

Learn More!

I am debugging a VBA macro which ends with the following error message:

«Run-time error ‘1004’:

«could not be found. Check the spelling of the file name, and verify
that the file location is correct.

If you are trying to open the file from your list of most recently used files, make sure that the file has not been remnamed, moved, or deleted.»

I checked that I was not using deleted/removed/renamed files and they were not coming from my Recent Files list.

The offending portion of the code which triggers the error message:

Workbooks.OpenText Filename:=vCPath _
, Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
, Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1)), _
TrailingMinusNumbers:=True

Here is all of the code:

' Changes to program to make
' Exclude outliers in average
' Develop fake data to at glance recognize whether program works.
' Source http://www.cpearson.com/excel/optimize.htm
' Declare .Filters
Option Explicit
Sub DataProcessingExperiment7()

On Error GoTo ErrorHandler
'   Declare as strings, as integers, as variants, decDecimals, as Office.FileDialog
Dim strPath, strFileN, strDirN, strRangeNOut, strRangeNIn, strFilename, strTLCorn, strBRCorn, strSelectedFile, strtemp_name As String
Dim vResMatrix(), vCPath, vFileN As Variant
Dim vRangeNOut, vRangeNIn, decInd6, decInd4, decInd5, decStep2, decMRow, decColIn, decInd3, decMcol As Double
Dim decMxRNo, decBgrSum, decRowIn, decInd, decM40eff, decStep, decColNo, decStartcol, decStartrow As Double
Dim decPlateNo, decMonoVal, decInd1, decEntryRow2, decEntryRow, decInd2, decBgrValP, decBgrRow As Double
Dim decBrgSum, decBgrVal, decRangeNIn, decRangeNOut, decTLCorn, decVolcorr, decBRCorn, decMEeff As Double
Dim decMediaVal, M40Eff, decMeanComp As Double
'   MEeff = measure of efflux due to crudely purified HDL in scintillation
'   Math operations are fastest with Integers / UIntegers: https://msdn.microsoft.com/en-us/library/ae55hdtk.aspx

'   Start File Explorer to select file containing data
Dim lngCount As Long
' Open the file dialog
With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = True
    .Show

'   Display paths of each file selected
    For lngCount = 1 To .SelectedItems.Count
    Next lngCount
    For Each strFilename In .SelectedItems
        MsgBox strFilename
    Next
End With

'   Excel 2003 is a good filter choice: Excel Viewer, OpenOffice, + Excel versions can open these files
'   If .Show = - 1 user picked at least one file
    ' The user pressed cancel.
'   Assign strings to param locations from (1,2) to (7,2). This matches template
'   Now using relative references to increase portability and NOT SELECTING the cells to increase macro speed, shorten code
With ActiveCell
    .Offset(1, 2) = decPlateNo
    .Offset(2, 2) = decStartrow
    .Offset(3, 2) = decStartcol
    .Offset(4, 2) = decColNo
    .Offset(5, 2) = decStep
    .Offset(6, 2) = strDirN
    .Offset(7, 2) = strFileN
End With

'   Measure of efflux of 2 plates of scint-40
M40Eff = 4.37
M40Eff = 2.4

'   Select and copy data and range
Range("A1:O22").Select
Selection.Copy
MsgBox "start"
'   Use the array
For decInd = 1 To decPlateNo / 2 - 1
    decRowIn = 1 + decStep * 2 * decInd
    decRangeNIn = "A" + CStr(decRowIn)
    Range(decRangeNIn).Select
    ActiveSheet.Paste
Next decInd 'Go to next indice
Application.CutCopyMode = False ' Have the data in both sheets

Workbooks.OpenText Filename:=vCPath _
, Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
, Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1)), _
TrailingMinusNumbers:=True

For decInd1 = 1 To decPlateNo 'Between these 2 columns
    decTLCorn = "B" + CStr(decStartrow + (decInd1 - 1) * decStep)
    decBRCorn = "M" + CStr(decStartrow + 7 + (decInd1 - 1) * decStep)
    decRangeNOut = decTLCorn + ":" + decBRCorn
    decRangeNIn = decTLCorn
    Windows(vFileN).Activate
    Range(vRangeNOut).Select
    Selection.Copy
    Windows(strtemp_name).Activate
    Sheets("Data").Select
    Range(vRangeNIn).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Next decInd1
'   Operation:=xlNone prevents calculations while pasting (less CPU intensive)
'   Don't skip blanks, don't transpose

'   Calculation of background and efflux for individual plates
For decInd1 = 1 To decPlateNo / 2
    decStep2 = (decInd1 - 1) * decStep * 2
    decBgrRow = decStartrow + 8 + decStep2
    decBgrValP = Cells(decBgrRow, 2).Value 'Background plate value
    For decInd2 = 0 To 7 'From column Ind2 to column 7 do multiplication
        decEntryRow = decStartrow + decInd2 + decStep2
        decEntryRow2 = decStartrow + decInd2 + decStep2 + 11
        decMediaVal = Cells(decEntryRow, 13).Value
        decMonoVal = Cells(decEntryRow2, 13).Value
        Cells(decEntryRow, 15).Value = decMEeff * 100 * decVolcorr * (decMediaVal - decBgrValP) / (decM40eff * decMonoVal + decVolcorr * decMEeff * (decMediaVal - decBgrValP))
    Next decInd2
Next decInd1
MsgBox "end"
'   calculation of average background for all media plates
decBgrSum = 0
For decInd1 = 1 To decPlateNo / 2
    decStep2 = (decInd1 - 1) * decStep * 2
    For decInd2 = 0 To 7
        decRowIn = decStartrow + decStep2 + decInd2
        decBgrSum = decBgrSum + Cells(decRowIn, 2).Value
    Next decInd2
Next decInd1
decBgrVal = decBgrSum / (4 * decPlateNo) 'The plates are split up into 4 (control (media) plates are 2 of them).
decBgrVal = Cells(2, 17).Value
'   Rearanging data and transferring to Results sheet
decMxRNo = 8 * decColNo
ReDim vResMatrix(1 To decMxRNo, 1 To decPlateNo)
Sheets("Data").Select
For decInd1 = 1 To decPlateNo
    decMcol = decInd1
    For decInd2 = 1 To 8
        decRowIn = (decInd1 - 1) * decStep + decStartrow + decInd2 - 1
        For decInd3 = 1 To decColNo
            decColIn = decStartcol + decInd3 - 1
            decMRow = (decInd3 - 1) * 8 + decInd2
            vResMatrix(decMRow, decMcol) = Cells(decRowIn, decColIn).Value
        Next decInd3
    Next decInd2
Next decInd1

Sheets("Results").Select
For decInd4 = 1 To decPlateNo
    For decInd5 = 1 To decMxRNo
        Cells(decInd5, decInd4).Value = vResMatrix(decInd5, decInd4) 'Put the values of the matrix in this range?
    Next decInd5
Next decInd4
Sheets("Results QC").Select
'   Select QC (quality control) calculation
Range("C81:C82").Select
Selection.Copy
For decInd = 1 To decPlateNo / 2 - 1
    decColIn = 2 + 2 * decInd + 1
    Cells(81, decColIn).Select
    ActiveSheet.Paste 'Paste values of the active sheet (C81:C82 range)
Next decInd

For decInd4 = 1 To decPlateNo
    decInd6 = decInd4 + 2
    For decInd5 = 1 To decMxRNo 'Put these cell values in the matrix
        Cells(decInd5, decInd6).Value = vResMatrix(decInd5, decInd4)
    Next decInd5
Next decInd4

For decInd4 = 1 To decPlateNo
decInd6 = decInd4 + 2
decMeanComp = 0.6 * Cells(81, decInd6).Value
For decInd5 = 1 To decMxRNo
    If Cells(decInd5, decInd6).Value < decMeanComp Then
    Cells(decInd5, decInd6).Interior.Color = RGB(255, 0, 0)
'           If the cell value is less than the average highlight them red as outliers. (More likely: from pipettes that didn't release)
    ElseIf Cells(decInd5, decInd6).Value > decMeanComp Then
    Cells(decInd5, decInd6).Interior.Color = RGB(7, 253, 56)
'           If the cell value is greater than the average highlight them green as outliers. (Unlikely unless )

    End If
Next decInd5
Next decInd4
MsgBox "4"
ErrorHandler:
MsgBox "Error Code" & Err.Number & vbNewLine & Err.Description, vbCritical, "Error Code" & Err.Number
End Sub
' Another function to continue arranging the data. Start by declaring all data types.
Sub Arrange_data()

On Error GoTo ErrorHandler

Dim strPath, strFileN, strDirN, strCPath, strRangeNOut, strRangeNIn, strTLCorn, strBRCorn As String
Dim decColIn4: decColIn4 = CDec(decColIn4)
Dim decInd5: decInd5 = CDec(decInd5)
Dim decInd6: decInd6 = CDec(decInd6)
Dim decPlateNo: decPlateNo = CDec(decPlateNo)
Dim decStartrow: decStartrow = CDec(decStartrow)
Dim decStartcol: decStartcol = CDec(decStartcol)


Sheets("Parameters").Select
decPlateNo = Cells(1, 2).Value
decStartrow = Cells(2, 2).Values
decStartcol = Cells(3, 2).Value
decColNo = Cells(4, 2).Value
decStep = Cells(5, 2).Value
decMEeff = 2.03
decM40eff = 4.37 'microscint 40=kind of solution the macrophages go in
decVolcorr = 2.4
decMxRNo = 8 * decColNo
ReDim vResMatrix(1 To decMxRNo, 1 To decPlateNo)

'   Select QC (quality control) data
Sheets("Results QC").Select
For decInd1 = 1 To decPlateNo
    decInd3 = decInd1 + 2
    For decInd2 = 1 To iMxRNo
        vResMatrix(decInd2, decInd1) = Cells(decInd2, decInd3).Value
    Next decInd2
Next decInd1
'   Transfer data for two methods of efflux calculations.
'   Create template of columns.
Sheets("Processed indiv").Select
Range("C2:E87").Select
Selection.Copy
For decInd = 1 To decPlateNo / 2 - 1
    decColIn = 3 + 3 * decInd
    Cells(2, decColIn).Select
    ActiveSheet.Paste
Next decInd
Application.CutCopyMode = False
'   Don't cut and copy: leave the values in the Excel sheet.
'    For disgorging values stored in matrix and calculate efflux.
For decInd1 = 1 To decPlateNo / 2
    decColIn1 = 3 + 3 * (decInd1 - 1)
    decColIn2 = decColIn1 + 1
    decColIn3 = decColIn1 + 2
    decBgrRow = (decInd1 - 1) * decStep * 2 + decStartrow + 8
    decInd4 = 2 * decInd1
    decInd3 = decInd4 - 1
    Cells(1, decStartcol + 3 * (decInd1 - 1)).Value = "Plate " + CStr(decInd1)
'       Get background value for the plate.
    Sheets("Data").Select
    decBgrValP = Cells(decBgrRow, 2)
    Sheets("Processed indiv").Select
'       Digorging values and calculating efflux.
    For decInd2 = 1 To iMxRNo
        decMediaVal = vResMatrix(decInd2, decInd3)
        decMonoVal = vResMatrix(decInd2, decInd4)
        Cells(2 + decInd2, decColIn1).Value = decMediaVal
        Cells(2 + decInd2, decColIn2).Value = decMonoVal
        Cells(2 + decInd2, decColIn3).Value = decMEeff * 100 * decVolcorr * (decMediaVal - decBgrValP) / (decM40eff * decMonoVal + decVolcorr * decMEeff * (decMediaVal - decBgrValP))
    Next decInd2
Next decInd1
'   Remove no data cells.
For decInd1 = 1 To decPlateNo / 2
    decColIn1 = 3 + 3 * (decInd1 - 1)
    For decInd2 = 3 To decMxRNo + 2
        If Cells(decInd2, decColIn1).Value = "" Then
            Cells(decInd2, decColIn1 + 1).Value = ""
            Cells(decInd2, decColIn1 + 2).Value = ""
        End If
    Next decInd2
Next decInd1
'   calculate data based on plate average.
'   Create template for columns.
Sheets("Processed by plate").Select
Range("C2:E87").Select
Selection.Copy
For decInd = 1 To decPlateNo / 2 - 1
    decColIn = 3 + 3 * decInd
    Cells(2, decColIn).Select
    ActiveSheet.Paste
Next decInd
Application.CutCopyMode = False
'   Prep for disgorging values stored in matrix and calculate efflux.
For decInd1 = 1 To decPlateNo / 2
    decColIn1 = 3 + 3 * (decInd1 - 1)
    decColIn2 = decColIn1 + 1
    decColIn3 = decColIn1 + 2
    decBgrRow = (decInd1 - 1) * decStep * 2 + decStartrow + 8
    decInd4 = 2 * decInd1
    decInd3 = decInd4 - 1
    Cells(1, decStartcol + 3 * (decInd1 - 1)).Value = "Plate " + CStr(decInd1)
'       Get background value for the plate.
    Sheets("Data").Select
    decBgrValP = Cells(decBgrRow, 2)
    Sheets("Processed by plate").Select
'       Digorging values and calculating efflux.
'       When does this for loop end?
    For decInd2 = 1 To decMxRNo
        Cells(2 + decInd2, decColIn1).Value = vResMatrix(decInd2, decInd3)
        Cells(2 + decInd2, decColIn2).Value = vResMatrix(decInd2, decInd4)
    Next decInd2
'       Get average value for monolayer. (Cells are in 1-cell deep layer.) ?
    decMonoVal = Cells(83, decColIn2).Value
    For decInd2 = 1 To decMxRNo
        decMediaVal = vResMatrix(decInd2, decInd3)
        Cells(2 + decInd2, decColIn3).Value = decMEeff * 100 * decVolcorr * (decMediaVal - decBgrValP) / (decM40eff * decMonoVal + decVolcorr * decMEeff * (decMediaVal - decBgrValP))
    Next decInd2
Next decInd1
'   Remove no data cells.
For decInd1 = 1 To decPlateNo / 2
    decColIn1 = 3 + 3 * (decInd1 - 1)
    For decInd2 = 3 To decMxRNo + 2
        If Cells(decInd2, decColIn1).Value = "" Then
            Cells(decInd2, decColIn1 + 1).Value = ""
            Cells(decInd2, decColIn1 + 2).Value = ""
        End If
    Next decInd2
Next decInd1
'   Replace cell count data with calculated efflux in the matrix.
Sheets("Processed indiv").Select
For decInd1 = 1 To decPlateNo / 2
    decColIn1 = 5 + 3 * (decInd1 - 1)
    decInd4 = 2 * decInd1
    For decInd2 = 1 To decMxRNo
        vResMatrix(decInd2, decInd4) = Cells(2 + decInd2, decColIn1).Value
    Next decInd2
Next decInd1
'Move data to duplicate comparison.
Sheets("Duplicate comparison").Select
Range("C2:N87").Select
Selection.Copy
For decInd = 1 To decPlateNo / 4 - 1
    decColIn = 3 + 12 * decInd
    Cells(2, decColIn).Select
    ActiveSheet.Paste
Next decInd
Application.CutCopyMode = False
For decInd1 = 1 To decPlateNo / 4
    decColIn1 = 3 + 12 * (decInd1 - 1)
    decColIn2 = decColIn1 + 2
    decColIn3 = decColIn1 + 3
    decColIn4 = decColIn1 + 5
    decInd3 = decInd1 * 4 - 3
    decInd4 = decInd3 + 1
    decInd5 = decInd3 + 2
    decInd6 = decInd3 + 3
    Cells(1, decStartcol + 12 * (decInd1 - 1)).Value = "Plate " + CStr(2 * decInd1 - 1)
    Cells(1, decStartcol + 3 + 12 * (decInd1 - 1)).Value = "Plate " + CStr(2 * decInd1)
    For iInd2 = 1 To iMxRNo
        Cells(2 + iInd2, iColIn1).Value = vResMatrix(decInd2, decInd3)
        Cells(2 + iInd2, iColIn2).Value = vResMatrix(decInd2, decInd4)
        Cells(2 + iInd2, iColIn3).Value = vResMatrix(decInd2, decInd5)
        Cells(2 + iInd2, iColIn4).Value = vResMatrix(decInd2, decInd6)
    Next decInd2
Next decInd1
'   Remove no data cells.
For decInd1 = 1 To decPlateNo / 4
    decColIn1 = 3 + 12 * (decInd1 - 1)
    decColIn2 = decColIn1 + 3
    For decInd2 = 3 To decMxRNo + 2
        If Cells(decInd2, decColIn1).Value = "" Then
        Cells(decInd2, decColIn1 + 1).Value = ""
        For decInd = 1 To 6
            Cells(decInd2, decColIn1 + 5 + decInd).Value = ""
        Next decInd
        End If
If Cells(decInd2, decColIn2).Value = "" Then
    Cells(decInd2, decColIn2 + 1).Value = ""
    For decInd = 1 To 6
        Cells(decInd2, decColIn2 + 2 + decInd).Value = ""
    Next iInd
End If
Next decInd2
Next decInd1
'   Prepare summary statistics.
decTL = decPlateNo * decMxRNo / 2
ReDim vResMatrix(1 To 4, 1 To iTL)
Sheets("Processed indiv").Select
decRowIn = 3 + decMxRNo
decRowIn2 = decRowIn + 1
For decInd1 = 1 To 3 * decPlateNo / 2
    For decInd2 = 1 To 3
        decRowIn3 = 3 * (decInd1 - 1) + iInd2
        decColIn = 2 + decRowIn3
        vResMatrix(1, decRowIn3) = Cells(decRowIn, decColIn).Value
        vResMatrix(2, decRowIn3) = Cells(decRowIn2, decColIn).Value
    Next decInd2
Next decInd1
Sheets("Processed by plate").Select
decRowIn = 3 + iMxRNo
decRowIn2 = decRowIn + 1
For decInd1 = 1 To 3 * decPlateNo / 2
    For decInd2 = 1 To 3
        decRowIn3 = 3 * (decInd1 - 1) + decInd2
        decColIn = 2 + decRowIn3
        vResMatrix(3, iRowIn3) = Cells(decRowIn, decColIn).Value
        vResMatrix(4, decRowIn3) = Cells(decRowIn2, decColIn).Value
    Next decInd2
Next decInd1
'   Put summary statistics in distribution comp.
Sheets("Distribution comp").Select
For decInd = 1 To 3 * decPlateNo / 2
    Cells(2 + iInd, 2).Value = vResMatrix(1, iInd)
    Cells(2 + iInd, 3).Value = vResMatrix(2, iInd)
    Cells(2 + iInd, 5).Value = vResMatrix(3, iInd)
    Cells(2 + iInd, 6).Value = vResMatrix(4, iInd)
Next decInd
'   Prepare plate by plate results.
Sheets("Duplicate comparison").Select
For decInd = 1 To iPlateNo / 4
    decMatrixStep = (iInd - 1) * iMxRNo
    decColIn = 9 + 12 * (iInd - 1)
    decColIn2 = iColIn + 2
    decColIn3 = iColIn + 1
    decColIn4 = iColIn + 3
    For decInd2 = 1 To decMxRNo
        decMRow = iMatrixStep + decInd2
'           Difference between duplicates converted into range.
        vResMatrix(1, iMRow) = Cells(2 + decInd2, decColIn).Value
        vResMatrix(2, iMRow) = Cells(2 + decInd2, decColIn2).Value / 2
        vResMatrix(3, iMRow) = Cells(2 + decInd2, decColIn3).Value
        vResMatrix(4, iMRow) = Cells(2 + decInd2, decColIn4).Value / 2
    Next decInd2
Next decInd
'   Transfer data to Summary.
Sheets("Summary").Select
'   Prepare sheet.
Range("B3:G82").Select
Selection.Copy
For decInd = 1 To decPlateNo / 4 - 1
    decRowIn = 3 + decMxRNo * iInd
    decRangeNIn = "B" + CStr(decRowIn)
    Range(decRangeNIn).Select
    ActiveSheet.Paste
    For decInd2 = 1 To decMxRNo
        Cells(decInd2 + decRowIn - 1, 1).Value = "Plate " + CStr(decInd + 1)
    Next decInd2
Next decInd
Application.CutCopyMode = False
'   Don't cut and copy
'   Distribute values.
For decInd = 1 To decTL
    iRowIn = iInd + 2
    Cells(decRowIn, 4).Value = vResMatrix(1, decInd)
    Cells(decRowIn, 5).Value = vResMatrix(2, decInd)
    Cells(decRowIn, 6).Value = vResMatrix(3, decInd)
    Cells(decRowIn, 7).Value = vResMatrix(4, decInd)
Next decInd
'   Transfer data to Matrix summary.
Sheets("Matrix summary").Select
'   Prepare page for data.
Range("A1:M11").Select
Selection.Copy
For decInd = 1 To decPlateNo / 4 - 1
    decRowIn = 1 + decStep * decInd
    decRangeNIn = "A" + CStr(decRowIn)
    Range(decRangeNIn).Select
    ActiveSheet.Paste
Next decInd
Application.CutCopyMode = False
'   Distribute data.
For decInd1 = 0 To iPlateNo / 4 - 1
    decPlateStep = decStep * decInd1
    decMatrixStep = iColNo * 8 * decInd1
    For decInd2 = 0 To iColNo - 1
        decColIn = decStartcol + decInd2
        decColStep = 8 * iInd2
        For iInd3 = 0 To 7
            iRowIn = iStartrow + iPlateStep + iInd3
            iMxElem = iMatrixStep + iColStep + iInd3 + 1
            Cells(decRowIn, decColIn).Value = vResMatrix(3, iMxElem)
        Next decInd3
    Next decInd2
Next decInd1
'   collect statistical parameters on efflux for unknown samples.
decRowNo = decPlateNo / 4
ReDim decEffluxMatrix(1 To iRowNo, 1 To 6)
decStartRowIn = 10
decStartColIn = 2
For decInd = 0 To iRowNo - 1
    decRowIn = iStartRowIn + iStep * iInd
    EffluxMatrix(iInd + 1, 1) = Cells(iRowIn, iStartColIn).Value
    EffluxMatrix(iInd + 1, 2) = Cells(iRowIn + 1, iStartColIn).Value
Next decInd
'collect statistical parameters on efflux for control samples.
Sheets("Data").Select
decStartRowIn = 10
decStartColIn = 15
For iInd = 0 To iRowNo - 1
    iRowIn = iStartRowIn + iStep * iInd * 4
    EffluxMatrix(iInd + 1, 3) = Cells(iRowIn, iStartColIn).Value
    EffluxMatrix(iInd + 1, 4) = Cells(iRowIn + 1, iStartColIn).Value
    EffluxMatrix(iInd + 1, 5) = Cells(iRowIn + 22, iStartColIn).Value
    EffluxMatrix(iInd + 1, 6) = Cells(iRowIn + 23, iStartColIn).Value
Next decInd
'   Ouput of statistical parameters on efflux
Sheets("Matrix summary").Select
decStartRowOut = 4
decStartColOut = 15
For decInd1 = 1 To decRowNo
    decRowOut = decStartRowOut + decInd1 - 1
    For decInd2 = 1 To 6
        decColOut = decStartColOut + decInd2 - 1
        Cells(decRowOut, decColOut).Value = EffluxMatrix(decInd1, decInd2)
    Next iInd2
Next decInd1
'Output into Matrix min-max
Sheets("Matrix min-max").Select
For decInd1 = 0 To decPlateNo / 4 - 1
    decPlateStep = decStep * decInd1
    decMatrixStep = decColNo * 8 * decInd1
    For decInd2 = 0 To decColNo - 1
        decColIn = decStartcol + iInd2
        decColStep = 8 * decInd2
        For decInd3 = 0 To 7
            decRowIn = decStartrow + decPlateStep + decInd3
            decMxElem = decMatrixStep + decColStep + decInd3 + 1
            Cells(decRowIn, decColIn).Value = vResMatrix(3, decMxElem)
        Next decInd3
    Next decInd2
Next decInd1

ErrorHandler:
MsgBox "Error detected" & vbNewLine & "Error" & Err.Number & ": " & Err.Description, vbCritical, "Error Handler: Error " & Err.Number
MsgBox "If you want to force the program to run, go to the line below and insert a ' mark to comment the line out." & vbNewLine & "On Error GoTo ErrorHandler", vbCritical, "Error Handler: Error " & Err.Number
End Sub

 Summary:

In this post, I have included the complete information about Excel runtime error 1004. Besides that I have presented some best fixes to resolve runtime error 1004 effortlessly.

To fix Runtime Error 1004 in Excel you can take initiatives like uninstalling Microsoft Work, creating a new Excel template, or deleting The “GWXL97.XLA” File. If you don’t have any idea on how to apply these methods then go through this post.

Here in this article, we are going to discuss different types of VBA runtime error 1004 in Excel along with their fixes.

What Is Runtime Error 1004 In VBA Excel?

Excel error 1004 is one such annoying runtime error that mainly encounters while working with the Excel file. Or while trying to generate a Macro in Excel document and as a result, you are unable to do anything in your workbook.

This error may cause serious trouble while you are working with Visual Basic Applications and can crash your program or your system or in some cases, it freezes for some time. This error is faced by any versions of MS Excel such as Excel 2007/2010/2013/2016/2019 as well.

To recover lost Excel data, we recommend this tool:

This software will prevent Excel workbook data such as BI data, financial reports & other analytical information from corruption and data loss. With this software you can rebuild corrupt Excel files and restore every single visual representation & dataset to its original, intact state in 3 easy steps:

  1. Download Excel File Repair Tool rated Excellent by Softpedia, Softonic & CNET.
  2. Select the corrupt Excel file (XLS, XLSX) & click Repair to initiate the repair process.
  3. Preview the repaired files and click Save File to save the files at desired location.

Error Detail:

Error Code: run-time error 1004

Description: Application or object-defined error

Screenshot Of The Error:

run-time error 1004

Don’t worry you can fix this Microsoft Visual Basic runtime error 1004, just by following the steps mentioned in this post. But before approaching the fixes section catch more information regarding runtime error 1004.

Excel VBA Run Time Error 1004 Along With The Fixes

EXCEL ERRORS

The lists of error messages associated with this Excel error 1004 are:

  1. VB: run-time error ‘1004’: Application-defined or object-defined error
  2. Excel VBA Runtime error 1004 “Select method of Range class failed”
  3. runtime error 1004 method range of object _global failed visual basic
  4. Excel macro “Run-time error ‘1004″
  5. Runtime error 1004 method open of object workbooks failed
  6. Run time error ‘1004’: Method ‘Ranger’ of Object’ Worksheet’ Failed
  7. Save As VBA run time Error 1004: Application defined or object defined error

Let’s discuss each of them one by one…!

#1 – VBA Run Time Error 1004: That Name is already taken. Try a different One

This VBA Run Time Error 1004 in Excel mainly occurs at the time of renaming the sheet.

If a worksheet with the same name already exists but still you are assigning that name to some other worksheet. In that case, VBA will throw the run time error 1004 along with the message: “The Name is Already Taken. Try a different one.”

VBA Run Time Error 1004 in Excel 1

Solution: You can fix this error code by renaming your Excel sheet.

#2 – VBA Run Time Error 1004: Method “Range” of object’ _ Global’ failed

This VBA error code mainly occurs when someone tries to access the object range with wrong spelling or which doesn’t exist in the worksheet.

Suppose, you have named the cells range as “Headings,” but if you incorrectly mention the named range then obviously you will get the Run Time Error 1004: Method “Range” of object’ _ Global’ failed error.

VBA Run Time Error 1004 in Excel 2

Solution: So before running the code properly check the name of the range.

# 3 – VBA Run Time Error 1004: Select Method of Range class failed

This error code occurs when someone tries to choose the cells from a non-active sheet.

 Let’s understand with this an example:

Suppose you have selected cells from A1 to A5 from the Sheet1 worksheet. Whereas, your present active worksheet is Sheet2.

At that time it’s obvious to encounter Run Time Error 1004: Select Method of Range class failed.

VBA Run Time Error 1004 in Excel 3

Solution: To fix this, you need to activate the worksheet before selecting cells of it.

#4 – VBA Runtime Error 1004 method open of object workbooks failed

This specific run time Error 1004 arises when someone tries to open an Excel workbook having the same workbook name that is already open.

In that case, it’s quite common to encounter VBA Runtime Error 1004 method open of object workbooks failed.

VBA Run Time Error 1004 in Excel 4

Solution: Well to fix this, first of all close the already opened documents having a similar name.

#5 – VBA Runtime Error 1004 Method Sorry We Couldn’t Find:

The main reason behind the occurrence of this VBA error in Excel is due to renaming, shifting, or deletion of the mentioned path.

The reason behind this can be the wrong assigned path or file name with extension.

When your assigned code fails to fetch a file within your mentioned folder path. Then you will definitely get the runtime Error 1004 method. Sorry, and We couldn’t find it.

VBA Run Time Error 1004 in Excel 5

Solution: make a proper check across the given path or file name.

#6 – VBA Runtime Error 1004 Activate method range class failed

Behind this error, the reason can be activating the cells range without activating the Excel worksheet.

This specific error is quite very similar to the one which we have already discussed above i.e Run Time Error 1004: Select Method of Range class failed.

VBA Run Time Error 1004 in Excel 6

Solution: To fix this, you need to activate your excel sheet first and then activate the sheet cells. However, it is not possible to activate the cell of a sheet without activating the worksheet.

Why This Visual Basic Runtime Error 1004 Occurs?

Follow the reasons behind getting the run time error 1004:

  1. Due to corruption in the desktop icon for MS Excel.
  2. Conflict with other programs while opening VBA Excel file.
  3. When filtered data is copied and then pasted into MS Office Excel workbook.
  4. Due to application or object-defined error.
  5. A range value is set programmatically with a collection of large strings.

Well, these are common reasons behind getting the VBA runtime error 1004, now know how to fix it. Here we have described both the manual as well as automatic solution to fix the run time error 1004 in Excel 2016 and 2013. In case you are not able to fix the error manually then make use of the automatic MS Excel Repair Tool to fix the error automatically.

Fix Runtime Error 1004

Follow the steps given below to fix Excel run time error 1004 :

1: Uninstall Microsoft Work

2: Create New Excel Template

3: Delete The “GWXL97.XLA” File

Method 1: Uninstall Microsoft Work

1. Go to the Task Manager and stop the entire running programs.

2. Then go to Start menu > and select Control Panel.

run time error 1004 (1)

3. Next, in the Control Panel select Add or Remove Program.

run time error 1004 (2)

4. Here, you will get the list of programs that are currently installed on your PC, and then from the list select Microsoft Work.

run time error 1004

5. And click on uninstall to remove it from the PC.

It is also important to scan your system for viruses or malware, as this corrupts the files and important documents. You can make use of the best antivirus program to remove malware and also get rid of the runtime error 1004.

Method 2: Create New Excel Template

Another very simple method to fix Excel runtime error 1004 is by putting a new Excel worksheet file within a template. Instead of copying or duplicating the existing worksheet.

Here is the complete step on how to perform this task.

1.Start your Excel application.

2. Make a fresh new Excel workbook, after then delete the entire sheets present on it leaving only a single one.

3. Now format the workbook as per your need or like the way you want to design in your default template.

4. Excel 2003 user: Tap to the File>Save As option

SAVE EXCEL FILE

OR Excel 2007 or latest versions: Tap to the Microsoft Office button after then hit the Save As option.

SAVE EXCEL FILE 1

5. Now in the field of File name, assign name for your template.

6. On the side of Save as type there is a small arrow key, make a tap on it. From the opened drop menu

  • Excel 2003 users have to choose the Excel Template (.xlt)

Create New Excel Template 1

  • And Excel 2007 or later version have to choose the Excel Template (.xltx)

Create New Excel Template 2

7. Tap to the Save.

8. After the successful creation of the template, now you can programmatically insert it by making use of the following code:
Add Type:=path\filename

Remarks: 

From the above code, you have to replace the path\filename with the complete path including the file name. For assigning the exact location of the sheet template you have just created.

Method 3: Delete The “GWXL97.XLA” File

Follow another manual method to fix Excel Runtime Error 1004:

1. Right-click on the start menu.

2. Then select the Explore option.

Excel Runtime Error 1004

3. Then open the following directory – C:\Program Files\MSOffice\Office\XLSTART

Excel Runtime Error 1004 (1)

4. Here you need to delete “GWXL97.XLA” file

Excel Runtime Error 1004 (2)

5. And open the Excel after closing the explorer

You would find that the program is running fine without a runtime error. But if you are still facing the error then make use of the automatic MS Excel Repair Tool, to fix the error easily.

Automatic Solution: MS Excel Repair Tool

MS Excel Repair Tool is a professional recommended solution to easily repair both .xls and .xlsx file. It supports several files in one repair cycle. It is a unique tool that can repair multiple corrupted Excel files at one time and also recover everything included charts, cell comments, worksheet properties, and other data. This can recover the corrupt Excel file to a new blank file. It is extremely easy to use and supports both Windows as well as Mac operating systems.

* Free version of the product only previews recoverable data.

Steps to Utilize MS Excel Repair Tool:

excel-repair-main-interface-1

stellar-repair-for-excel-select-file-2

stellar-repair-for-excel-repairing-3

stellar-repair-for-excel-preview-4

stellar-repair-for-excel-save-5

stellar-repair-for-excel-saving-6

stellar-repair-for-excel-repaired-7

previous arrow

next arrow

Conclusion:

Hope this article helps you to repair the runtime error 1004 in Excel and recovers Excel file data. In this article, we have provided a manual as well as automatic solution to get rid of Excel run-time error 1004. You can make use of any solution according to your desire.

Good Luck!!!

Priyanka is a content marketing expert. She writes tech blogs and has expertise in MS Office, Excel, and other tech subjects. Her distinctive art of presenting tech information in the easy-to-understand language is very impressive. When not writing, she loves unplanned travels.

VBA Runtime Error 1004 occurs while you are executing a macro in Excel. It’s an error that can occur due to several reasons. In the below example, as you can see, when I run the code, it shows the run-time error ‘1004’. In simple words, you can also say it occurs when you can’t perform an activity with the object or the application.

vba-run-time-error

In this tutorial, we will understand several other reasons for this runtime 1004 error.

Error1: VBA Run Time Error 1004: That Name is already taken. Try a different One:

As I shared above, when you try to add a sheet with the name that is already there in the workbook using a VBA code, VBA shows you a run-time error with the 1004 code.

Even when you try to rename a worksheet, you’ll get the same error from VBA.

In the above code, I tried to rename the sheet “Sheet2” with the name “Sheet1”, but as you can see, I already have the “Sheet1” in the workbook; VBA showed me the error code 1004.

To deal with this problem is to check if a sheet already exists in the workbook or not.

Error2: VBA Run Time Error 1004: Method “Range” of object’ _ Global’ failed:

As you know, you can create a named range in Excel, but when you try to refer to that named range using a VBA code, and you misspelled its name, you are likely to get the run-time error 1004 Method “Range” of object’ _ Global’ failed.

In the following example, I have a named range with the name “myRange”, but while writing the macro, I have misspelled it as “myRang” even though it’s a typing mistake; VBA is not that smart to identify this mistake, and refer to the correct named range.

There is no named range with the name that I have mentioned, and that is why it showed me the run-time error 1004.

Error3: VBA Run Time Error 1004: Select Method of Range class failed:

When you use a VBA code to select a range that is not in the active worksheet, VBA will show you the run-time error 1004. Because, if you want to select a cell or range of cells for a particular worksheet needs to be active at that point.

You cannot select a cell from sheet1 if sheet2 is active right now.

As you can see in the following example, I have made the same mistake where I’m referring to cell A1 from sheet one, but sheet2 is active.

The best way to deal with this error is to activate the sheet first and then write a line of code that selects a cell or the range from that sheet. You can also check if a sheet is active or not and then select the cell from that sheet, and if somehow that sheet is not active, you can show a message to the user to activate that sheet first.

Error4: VBA Runtime Error 1004 Activate method range class failed:

Just like the above reason, when you try to activate a cell or range of cells from a specific worksheet that is not active at the moment, we will show you runtime error 1004 (Activate method range class failed:).

In the about example when I tried to activate cell A1 in the sheet1 it gave me runtime error 1004, is the active sheet was the sheet2.

To deal with this error again you need to verify that the worksheet that you are referring to is active or not. Or you can also verify if that sheet is there in the workbook or not.

Error5: VBA Runtime Error 1004 method Sorry We couldn’t Find:

If you’re trying to open an Excel file using a path, but that workbook is not there, maybe it has been moved, deleted, or renamed. In that case, VBA will again show you a run-time error of 1004.

That’s what happened in the following example where I tried to open the file with the path from my desktop, but that file is not there, and it has shown me an error.

To deal with the situation, you can use a VBA code to verify if the file that you are referring to is there in the folder or not.

Error6: VBA Runtime Error 1004 method open of object workbooks failed:

This error is also related to the workbook. So when you try to open a workbook with the name for which a book already exists and is open at the moment, VBA will show you the run-time error 1004 (method open of object workbooks failed).

The same thing happened in the above example, where I tried to open a workbook that is already open, and VBA showed me a run time error (1004).

How to Correct VBA’s Runtime (Error 1004)

Error 1004 is a run-time error that occurs while executing a VBA code. And all the errors that we have discussed above are due to human errors while writing the code, following the points you can use to avoid the run-time error 1004.

  1. Do a step-by-step code execution to see on which line the error occurs.
  2. Make sure to verify all the names of the workbooks that you are referring to.
  3. You can also use a VBA code to verify if a workbook is already opened or not.
  4. And use the activate method to activate a worksheet before you refer to a cell or range from it.

What is VBA

  • VBA ERROR Handling
  • VBA Automation Error (Error 440)
  • VBA Error 400
  • VBA Invalid Procedure Call Or Argument Error (Error 5)
  • VBA Object Doesn’t Support this Property or Method Error (Error 438)
  • VBA Object Required Error (Error 424)
  • VBA Out of Memory Error (Error 7)
  • VBA Overflow Error (Error 6)
  • VBA Subscript Out of Range Runtime Error (Error 9)
  • VBA Type Mismatch Error (Error 13)

Понравилась статья? Поделить с друзьями:
  • Ошибка при вызове конструктора записьтекста 1с
  • Ошибка при входе на портал госуслуги
  • Ошибка при вызове конструктора двоичныеданные каталог не обнаружен
  • Ошибка при входе на кристаликс
  • Ошибка при выполнении команды старлайн