运行时错误“1004":对象“_Global"的方法“范围"失败 [英] Run-time error '1004' : Method 'Range' of object'_Global' failed
问题描述
我在使用 excel 时遇到问题,表单生成参考编号.但是当我尝试生成参考号时.它有一条错误消息说:
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 :
运行时错误'1004':对象'_Global'的方法'Range'失败
Run-time error '1004' : Method 'Range' of object'_Global' failed
当我点击调试按钮时,它显示如下代码:
When I click on Debug button , it shows the code as below:
突出显示代码第 4 行的错误
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?
推荐答案
当您像这样引用 Range 时,它被称为非限定引用,因为您没有具体说明 Range 在哪个工作表上.不合格的引用由_Global"对象处理,该对象决定您所引用的对象,这取决于您的代码所在的位置.
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.
如果您在标准模块中,未限定的范围将引用 Activesheet.如果您在工作表的类模块中,未限定的范围将引用该工作表.
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 是一个变量,它包含对一个范围的引用,可能是一个命名范围.如果您查看该命名范围的 RefersTo 属性,它可能会在代码执行时指向 Activesheet 以外的工作表.
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.
这篇关于运行时错误“1004":对象“_Global"的方法“范围"失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!