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

前端 未结 1 1079
终归单人心
终归单人心 2020-11-27 20:23

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 :

相关标签:
1条回答
  • 2020-11-27 20:54

    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.

    0 讨论(0)
提交回复
热议问题