Ошибка vba method range of object global failed

I have a problem with excel, with a form that generates a reference no. But when I try to generate the reference no. it has an error message saying :

Run-time error ‘1004’ : Method ‘Range’ of object’_Global’ failed

When I click on Debug button , it shows the code as below:

It highlight the error on 4th line of the code

Sub clearTemplate()
    ' Clear Template Content
    Range(inputTemplateHeader) = NO_ENTRY
    Range(inputTemplateContent) = NO_ENTRY     - (highlighted error)
End Sub

Sub clearRefNo()
    ' Clear cell G2 reference number
    Range(cellRefNo) = NO_ENTRY

    ' Open "Report_ref_no.xls"
    If Not (IsFileOpen) Then Workbooks.Open filename:=ThisWorkbook.Path & "" & FACCESS

    ' Activate "Report_ref_no.xls"
    Windows(FACCESS).Activate

    ' Access column D
    Range(cellFirstRefNo).Select
    Selection.End(xlDown).Select

    If refNo = Cells(ActiveCell.Row, ActiveCell.Column - 1).Value Then
        ' Log Development Code column
        Cells(ActiveCell.Row, ActiveCell.Column) = NO_ENTRY

        ' Log Issuer column
        Cells(ActiveCell.Row, ActiveCell.Column + 1).Value = NO_ENTRY

        ' Log Date column
        Cells(ActiveCell.Row, ActiveCell.Column + 2).Value = NO_ENTRY
    End If

    ' Save & Close workbook
    ActiveWindow.Close True
End Sub

Can anyone help me with this problem ? I don’t know what has gone wrong?

Community's user avatar

asked Aug 29, 2012 at 9:26

Melissa Charn's user avatar

2

When you reference Range like that it’s called an unqualified reference because you don’t specifically say which sheet the range is on. Unqualified references are handled by the «_Global» object that determines which object you’re referring to and that depends on where your code is.

If you’re in a standard module, unqualified Range will refer to Activesheet. If you’re in a sheet’s class module, unqualified Range will refer to that sheet.

inputTemplateContent is a variable that contains a reference to a range, probably a named range. If you look at the RefersTo property of that named range, it likely points to a sheet other than the Activesheet at the time the code executes.

The best way to fix this is to avoid unqualified Range references by specifying the sheet. Like

With ThisWorkbook.Worksheets("Template")
    .Range(inputTemplateHeader).Value = NO_ENTRY
    .Range(inputTemplateContent).Value = NO_ENTRY
End With

Adjust the workbook and worksheet references to fit your particular situation.

answered Aug 29, 2012 at 12:54

Dick Kusleika's user avatar

Dick KusleikaDick Kusleika

32.6k4 gold badges51 silver badges73 bronze badges

Mr. Mickle

Thank you for the new tip.  A new horizon….

the final SUB would be below. ( It worked nicely )

(it is case sensitive)

many many thanks

Sub RemoveWordTotal()
Application.ScreenUpdating = False
Dim i As Long
For i = Cells(Rows.Count, "B").End(xlUp).Row To 2 Step -1
Cells(i, "B") = Application.Substitute(Cells(i, "B"),"Total","")
Next i
Application.ScreenUpdating = True
End Sub

The above can also be used to replace a text instead of just blank.
instead of "" - replace it with "New Text" !!
VERY VERSATILE CODE, Mr. Mickle..

FOR OTHERS WHO MIGHT FIND THIS USEFUL:
maybe called as function (BELOW) > Call ReplaceSomeText("Total", "B", "")
"Total" (mWRD) = the word within the cell to be replaced
"B" (mcol) = the column referenced
"" (newTXT) = blank or any text to replace mWRD

Sub ReplaceSomeText(mWRD As String, mcol As String, newTXT As String)
Application.ScreenUpdating = False
Dim i As Long
For i = Cells(Rows.Count, mcol).End(xlUp).Row To 2 Step -1
Cells(i, mcol) = Application.Substitute(Cells(i, mcol),mWRD, newTXT)
Next i
Application.ScreenUpdating = True
End Sub

В макросе происходит ошибка «VBA method range of object _global failed» при вставке чисел в нужный диапазон.
Ошибка в этой строке:
Range(«28 суток!D» & y).Value = i

Почему происходит ошибка?

Sub uniqb25()
    Const min = 33.01
    Const max = 39.99
        For y = 5 To 24
            i = ((max — 2) — min + 1) * Rnd + (min + 1)
            Range(«28 суток!D» & y).Value = i
        Next y
End Sub

Method Range of object _global failed error says that you aren’t satisfying the argument expectations of the Range() method. The given method asks for a particular type of input, and its demand needs to be fulfilled irrespective of your coding logic.Details of Method Range of Object Global Failed

As there can be different coding scenarios, the following post will discuss the argument expectations of the Range() method and tell you about different ways through which you can fix the said error. So, spend the next few minutes reading this article, and you’ll be thankful to have found this comprehensive guide.

Contents

  • Why Is the Method Range of Object _global Failed Error Arising?
    • – An Unexpected Range Reference
  • How To Fix Method Range of Object _global Failed Error?
    • – Offer the Correct Range Reference
    • – Work With the R1C1 Notation
    • – Replace Range() With Cells()
    • – Leverage the Address Property of the Range() Method
  • FAQ
    • 1. What Causes the Method Open Object Workbooks Failed Error?
    • 2. What Does the Select Method of Range Class Failed Error Mean?
    • 3. How To Fix Method ‘ExecuteExcel4Macro’ of Object’_global’ Failed?
    • 4. How To Resolve the Object Does Not Exist Error?
    • 5. What Are the Steps To Name a Range in VBA?
  • Conclusion

Why Is the Method Range of Object _global Failed Error Arising?

The method Range of object ‘global’ failed VBA runtime error is arising due to the unfulfilled argument expectations of the Range() method. If you don’t provide a correct range reference to the given method, the method call will fail, and you’ll see the same error occurring on your PC.

– An Unexpected Range Reference

Providing an unexpected or wrong range reference to the Range() method can become a reason for the method’ range of object _global failed Excel VBA error. It will mostly occur when you pass a combination of the row and column numbers to the Range() method.Method Range of Object Global Failed Error

Also, you can say that the situations where you don’t pass the range directly and expect some processed numeric values to create your desired range of cells are most likely to lead you to the same error. However, it doesn’t mean that you shouldn’t do any processing beforehand and always go for the direct approach. All that matters is the correctness of the resulting range reference, where a single cell is depicted by a column letter and a row number.

Look at this example to gain clarity. You have created a function where you process some values and create variables like targetRow and targetColumn containing row and column numbers. Now, you want to target a particular cell by passing the said variables to the Range() method. Here, you might face the given run-time error because both of the variables together result in a numeric value, while the given method expects the column to be represented by an alphabet or letter.

How To Fix Method Range of Object _global Failed Error?

You can fix the method ‘range’ of object failed VBA error by passing a correct reference range to the Range() method, or switching to the Cells() method. To satisfy the Range() method, you can use the R1C1 reference style or leverage the Address property of the same method.

– Offer the Correct Range Reference

You should offer a correct range reference to the Range() method to ensure that the method call doesn’t fail and you don’t get the stated error on your system. So, to satisfy the given method, you’ll have to specify a column letter and a row number to represent each cell.

Talking about the example discussed earlier. You’ll need to ensure that after performing your desired operations, the variable targeting a particular column contains an alphabet, while the targetRow variable contains a number. This way even if your Range() method call looks like Range(targetColumn & targetRow), you’ll not get the method’ range of object _global’ failed table error.

– Work With the R1C1 Notation

The R1C1 notation can help you use the column number instead of the column letter flawlessly. It will work like extra information provided to the Range() method about the variables passed to it. So, you’ll only need to precede the variables with R and C to fix the error.

It won’t be wrong to say that introducing the R1C1 notation to your Range() method argument will reduce the hassle of converting the column number to a column letter, as suggested by the previous solution. Here you go with a code snippet that adds the said notation to the Range() method call.

Range(“R” & targetRow & “C” & targetColumn)

Now, the stated method will know that the number stored in the targetRow is the number of the row, and the number stored in the targetColumn is the column number. Consequently, it won’t be confused anymore, and you’ll not get the given error. To enhance your knowledge, note that R1C1 notation is also known as the R1C1 reference style.

– Replace Range() With Cells()

If you are trying to obtain or place the data in a particular cell by using the Range() method, and the error is constantly disturbing you, then you should switch to the Cells() method. The Cells() method will make it easier for you to target the row and column numbers.Fix Method Range of Object Global Failed Error

The given method accepts row number and column number separated by a comma and lets you target a particular cell. It means that you’ll neither need to use the R1C1 reference style nor convert the column number into the column letter. All that you’ll have to do is to execute the Cells method and say no to the Range() method.

You can look at the implementation of the Cells() method below:

Cells(targetRow,targetColumn)

– Leverage the Address Property of the Range() Method

You can use the Address property of the Range() method to kill the method ‘range’ of object’_worksheet’ failed 1004 error on the go. The said property can be helpful when your code calls the Cells() method as an argument of the Range() method.

Think about it this way. You have written your VBA code in such a way that the Range() method receives a call to the Cells() method as an argument. Now, although the latter method returns the expected cell address, it will not be acceptable by the former method unless you use the Address property.

Here is how you can use the Address property with the combo of Range() and Cells() methods:

Ws.Range(Ws.Cells(targetRow, targetColumn).Address)

FAQ

1. What Causes the Method Open Object Workbooks Failed Error?

The method Open object of workbooks failed error might be caused by the corrupted temporary files or an Access file referencing an Excel file in the same OneDrive folder. Also, there can be other reasons depending on your code block.

2. What Does the Select Method of Range Class Failed Error Mean?

The Select method of Range class failed means that you are trying to select the cells without activating the worksheet where they are present. It’s like ignoring the sheet activation or selection step and jumping to the cell selection step directly.

3. How To Fix Method ‘ExecuteExcel4Macro’ of Object’_global’ Failed?

You can fix the method ‘ExecuteExcel4Macro’ of object’_global’ failed error by specifying the workbook and worksheet references. It will work because Microsoft Excel 4.0 macro is successfully evaluated in the external worksheet or workbook and can’t be used in the context of the current worksheet.

4. How To Resolve the Object Does Not Exist Error?

You can resolve the VBA Error 1004: Object does not exist error by creating the range names in the Excel workbook. Also, referring to the range in the traditional row and column format like Range(“A1:B10”) will help fix the error.

5. What Are the Steps To Name a Range in VBA?

You can name a range by defining a variable as Range and setting it to your desired range of cells. Next, you’ll have to access the Names property of ThisWorkbook object. Later, you’ll have to specify the Name argument and the range variable as the RefersTo argument.

Conclusion

According to the above post, the method range of object _global failed error occurs as a signal showing that you haven’t passed a valid reference range to the Range() method. Remember that the Range() method accepts a column letter and a row number and will throw this error when it doesn’t see any letter that would indicate the target column. Please read the below listicle to grab the important details from the article quickly.

  • You should double-check the arguments being passed to the Range method VBA and ensure that it’s receiving a valid column letter followed by a valid row number to resolve the error on your screen.
  • You can use the R1C1 notation or reference style to clarify the Range() method about the row and column numbers.
  • It would be best to use the Cells() method when you want to target a specific cell with the help of a column number and row number.
  • You can use the Address property of the Range() method to extract the acceptable cell address from the result of the Cells() method.

Now, you are all clear about the Range() method argument and can safely resume your work by beginning with resolving the said error.

  • Author
  • Recent Posts

Position is Everything

Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team

Position is Everything

This is one of the most common run-time errors. By the end of this article, you will understand why that is and how to fix it. A run-time error is the type of error that occurs during the execution of the code. VBA does not know about it prior to actually running the code. There are different variations of this error; we will provide an example of each of the types below.

EXAMPLE 1: VBA Runtime Error 1004: Method ‘Range’ of object ‘_ Global’ failed

When a range reference is not correct. It could be incorrect because it’s misspelled. It could also be incorrect because it’s trying to get a range that is at an impossible value, such as row 0 or row -2. In the example below, we’re referring to row zero and we’re using the wrong syntax. VBA recognizes that we’re trying to refer to a range but it sees that the arguments (parameters) written within the range in order to identify it are written incorrectly; there should be no commas when using Range().

Sub Range_Error()
Range(0, 1).Select
End Sub

range of object global failed

Instead, we should write the code as follows:

Sub Range_Error()
Range(“A1”).Select
End Sub

We could also run into this problem unintentionally if we are looping a variable, such as X, and it becomes zero.

In the below example, X is equal to 5 and the loop is set to keep reducing X by 1 until it reaches -1 then it would not execute. But an error will occur when X = 0.

I set the code to write ‘Correct’ in each of the cells up until the cell we are not able to insert values into. Our code will insert ‘Correct’ in Range(“A5) to Range(“A1”) then will give the error when we ask it to insert ‘Correct’ into Range(“A0”).

Note that we didn’t define X to be a String, so VBA assumes it’s a variant. This means it can be any type of data, so Excel assumes that it’s the type of data that should be utilized in the context we’re utilizing it in. In this case, X would be considered a string. But when X is zero, Excel doesn’t know that it should be string, since “A0” doesn’t exist as a range. So it treats the zero as a number, which triggers this error, because it’s syntax related, meaning what’s written inside Range() is not written correctly from a syntax point of view.

Sub Range_Error()
X = 5
Do Until X = -1
Range("A" & X) = "Correct"
X = X - 1
Loop
End Sub

method range of object global failed

To correct the problem, we need to stop our code from running the loop once X reaches 0.

Sub Range_Error()
X = 5
Do Until X = 0
Range("A" & X) = "Correct"
X = X - 1
Loop
End Sub

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

This error is shown when we are trying to give the same name to a worksheet which has already been given to another worksheet in the same workbook. Given that we already have a sheet named “Sheet1”, we create a new sheet, we click on it so that it becomes the active sheet, and we try to name it “Sheet1” by running the below code. We get an error immediately once we reach the sheet name changing line.

Sub Error_Name_Taken()
ThisWorkbook.Sheets("Sheet2").Activate
ActiveSheet.Name = "Sheet1"
End Sub

name already taken

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

This error occurs when we attempt to activate a range through VBA in another worksheet (not the currently active one) without activating that worksheet first.

Sub Error_Select_Failed()
ThisWorkbook.Sheets("Sheet2").Activate
ThisWorkbook.Sheets("Sheet1").Range("A1").Select
End Sub

select method of range class failed

To correct the issue, we would need to first activate “Sheet1” first before attempting to select a range inside of it. Adding a line to active the worksheet resolves the issue.

Sub Error_Select_Failed()
ThisWorkbook.Sheets("Sheet2").Activate
ThisWorkbook.Sheets("Sheet1").Activate
ThisWorkbook.Sheets("Sheet1").Range("A1").Select
End Sub

EXAMPLE 4: VBA Runtime Error 1004: Method ‘Open’ of object ‘Workbooks’ failed

This error occurs when we use VBA to open a workbook that cannot be opened. The reason that Excel is unable to open the workbook could be because it is already open, or the workbook is being used by another program at that moment. A special case exists when you attempt to open a file in read-only mode while the file is corrupt. This would give an error because Excel can open a corrupt file only if it is allowed to repair it first, which it does by writing to it. So, if you open a corrupt file in read-only mode, you are essentially preventing it from repairing the file and hence it would fail to open.

EXAMPLE 5: VBA Runtime Error 1004: file format is not valid

This error, like the one in example 5, is related to attempting to open a file. In this situation, however, we are trying to open a file that isn’t an Excel file; the file doesn’t have any of the Excel extensions ( .xlsx, .xls, .xlsb, .xlsm, etc.)

Sub error_workbook_open()
Workbooks.Open "C:UsersmmostDropboxDaniel TrohaRun-time error 1004.docx"
End Sub

file format is not valid

The code is attempting to open a word document file with extension .docx. Using ‘Workbooks.Open’ can only be used for files that have Excel extensions. To open documents of different extensions, we will need to take a completely different approach. For example, the below code opens a word document.

Sub error_word_doc_open()
Dim wordapp
Dim strFileName As String
strFileName = "C:UsersmmostDropbox Else Without If.docx"
Set wordapp = CreateObject("word.Application")
wordapp.Documents.Open strFile
wordapp.Visible = True
End Sub

EXAMPLE 6: VBA Runtime Error 1004: Sorry We Couldn’t Find:

This error occurs whenever we are trying to open a file that does not exist at the specified path. When faced with this error, it’s important to check three aspects of the file path; file location, file name, and file extension. Any of these parameters could be wrong, so check each of them carefully.

Sub error_workbook_open()
Workbooks.Open "C:UsersmmostDropboxAnother Workbook.xls"
End Sub

couldn't find workbook

EXAMPLE 7: VBA Runtime Error 1004: Application-defined or Object-defined error

This error encompasses a wide range of possibilities. The error is triggered due to violating one of the rules that are used to handle the object you’re working with.

For example, in the following code, I try to select a cell that exists on row -1 and column 1. There is no such cell as the minimum row number allowed is 1, thus, VBA determines that I violated this rule and throws this error.

Note that this is different from Example 1 in that the syntax I am using here is perfectly correct, but I am violating particular rules that once violated, trigger an error. The wide range of these rules make this one of the most common errors in VBA. To resolve it, you will want to know more about the object you’re working with (in this case it’s a cell in the worksheet) and understand the rules and limitations that exist on the various values that you can use with the object.

Sub error_object_defined()
ThisWorkbook.Sheets(1).Cells(-1, 1).Select
End Sub

application defined or object defined error

To wrap it up, here’s one example of where a syntax error calling a function causes this error to occur.

  • Ошибка vba else without if else
  • Ошибка v107 пассат б6
  • Ошибка v0100 сузуки гранд витара
  • Ошибка vba 1004 при сохранении файла
  • Ошибка variable used without being declared