vba 运行时错误“1004”:对象“_Global”的方法“Range”失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12174723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run-time error '1004' : Method 'Range' of object'_Global' failed
提问by Melissa Charn
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 :
我在使用 excel 时遇到问题,表单会生成参考编号。但是当我尝试生成参考号时。它有一条错误消息说:
Run-time error '1004' : Method 'Range' of object'_Global' failed
运行时错误“1004”:对象“_Global”的方法“Range”失败
When I click on Debug button , it shows the code as below:
当我点击调试按钮时,它显示的代码如下:
It highlight the error on 4th line of the code
它突出显示代码第 4 行的错误
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?
谁能帮我解决这个问题?不知道出了什么问题?
回答by Dick Kusleika
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.
当您像这样引用 Range 时,它被称为非限定引用,因为您没有具体说明该范围位于哪个工作表上。不合格的引用由“_Global”对象处理,该对象确定您所引用的对象,这取决于您的代码所在的位置。
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.
如果您在标准模块中,未限定的范围将引用 Activesheet。如果您在工作表的类模块中,未限定的 Range 将引用该工作表。
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.
inputTemplateContent 是一个变量,它包含对一个范围的引用,可能是一个命名范围。如果您查看该命名范围的 RefersTo 属性,它可能会在代码执行时指向 Activesheet 以外的工作表。
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.
调整工作簿和工作表引用以适合您的特定情况。