简体   繁体   English

对象'_Global'的方法'Range'在第二次运行时失败

[英]Method 'Range' of Object'_Global' Failed when run for the 2nd time

I am having a problem with the error:我遇到错误问题:

Method 'Range' of Object'_Global' Failed对象'_Global'的方法'范围'失败

... on a section of code when it runs for the second time. ...第二次运行时的一段代码。

The entire program is to export information from an SQL database via access queries and create the source table that is then exported to an excel spreadsheet, the source table contains information from 14 different locations.整个程序是通过访问查询从 SQL 数据库中导出信息并创建源表,然后导出到 excel 电子表格,源表包含来自 14 个不同位置的信息。 The excel spreadsheet is therefore created 14 times (and is eventually emailed to 14 different people)因此,Excel 电子表格创建了 14 次(并最终通过电子邮件发送给 14 个不同的人)

For the purpose of testing the code I have put it in a Do While loop to simulate the code having to run more than once, I have not done any programming in a few decades so I am very rusty, the section of code that is failing is:为了测试代码,我把它放在一个 Do While 循环中来模拟必须运行多次的代码,几十年来我没有做过任何编程,所以我很生疏,那部分代码失败了是:

.Cells.EntireRow.EntireColumn.Sort key1:=Range("G2"), order1:=xlDescending, Header:=xlYes

Full code:完整代码:

Private Function Open_Excel_Spreadsheet()

    Dim Cnt As Integer 'Counter
    Cnt = 1

    Do While Cnt < 4

        Dim oExcel      As Excel.Application
        Dim oBook       As Excel.Workbook
        Dim oSheet      As Excel.Worksheet
        Dim LastRow     As Long
        Dim FirstNewRow As Long
        Dim i As Integer 'Row counter in the range of 2 to FirstNewRow
        i = 2

        Set oExcel = CreateObject("Excel.Application")
        Set oBook = oExcel.Workbooks.Open([MyDocsPath], , ReadOnly:=False)
        Set oSheet = oBook.Worksheets(1)

        oExcel.Visible = True

        'Find the last used row in Column A and set LastRow and FirstNewRow variables
        With oSheet
            LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
        FirstNewRow = LastRow + 1

        Debug.Print Cnt; FirstNewRow

        'Format the Spreadsheet
        With oSheet
            .Sort.SortFields.Clear

            ' Next line generates "Method 'Range' of Object'_Global' Failed" the second time it runs
            .Cells.EntireRow.EntireColumn.Sort key1:=Range("G2"), order1:=xlDescending, Header:=xlYes

            .Cells(FirstNewRow, 5) = "=sum(E2:E" & [LastRow] & ")"
            .Cells(FirstNewRow, 6) = "=sum(F2:F" & [LastRow] & ")"
            For i = 2 To FirstNewRow
                .Cells(i, 7) = "=IF(+E" & [i] & "=0,0,Round(((+E" & [i] & "-F" & [i] & ")/E" & [i] & ")*100,2))"
            Next i
            .Range("E1:G1").EntireColumn.NumberFormat = "#,##0.00_);[Red]-#,##0.00"
            .Range("a1").EntireRow.EntireColumn.AutoFit
        End With

        'Save Workbook and drop Excel
        oBook.Close True

    Cnt = Cnt + 1
    Loop

End Function

As it sits, Range("G2") may or may not be referring to the G2 cell on the oSheet worksheet.实际上, Range("G2")可能指也可能不指oSheet工作表上的 G2 单元格。 The only thing that is certain is that it refers to the G2 cell on the worksheet that currently holds the ActiveSheet property .唯一可以确定的是,它指的是工作表上当前拥有ActiveSheet 属性的 G2 单元格。

Try it as,试试看,

.Cells.EntireRow.EntireColumn.Sort key1:=.Range("G2"), order1:=xlDescending, Header:=xlYes

Note .Range("G2") and not Range("G2") .注意.Range("G2")而不是Range("G2") This explicitly references the G2 on the worksheet referenced with the With... End With statement .这明确引用了With... End With 语句引用的工作表上的 G2。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时错误&#39;1004&#39;:对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Run-time error '1004' : Method 'Range' of object'_Global' failed 运行时错误&#39;1004&#39;:对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Run-time error '1004': Method 'Range' of object '_Global' failed 运行时错误1004-对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Run-time error 1004 - Method 'Range' of object'_Global' failed 运行时错误1004对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Run Time error 1004 Method 'Range' of object '_Global' failed 运行时错误&#39;1004&#39;对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Run-time error '1004' Method 'Range' of object'_Global' failed 运行时错误&#39;1004&#39;:对象&#39;_Global&#39;的方法&#39;Range&#39;失败6 - Run-time error '1004' : Method 'Range' of object'_Global' failed 6 运行时错误'1004' - 对象'_Global'的方法'范围'失败 - Run-time error '1004' - Method 'Range' of object'_Global' failed 运行时错误1004。方法&#39;范围&#39;对象&#39;_Global&#39;_失败 - Run time error 1004. Method 'Range' object '_Global' _ failed 运行时错误“1004”-object“_Global”的方法“范围”失败 - Run-time error '1004' - Method 'Range' of object '_Global' failed 运行时错误&#39;1004&#39;-尝试隐藏列时对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Run-time error '1004' - Method 'Range' of object'_Global' failed when trying to hide columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM