运行时错误'1004':对象“_全局”的方法“范围”失败 [英] Run-time error '1004' : Method 'Range' of object'_Global' failed

查看:344
本文介绍了运行时错误'1004':对象“_全局”的方法“范围”失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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'的方法'范围'失败

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

当我点击Debug按钮,它显示如下代码:

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时,它被称为不合格的引用,因为你不t具体说明范围是哪一张。不合格的引用由_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':对象“_全局”的方法“范围”失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆