方法_Global对象的范围失败

时间:2018-03-12 19:59:24

标签: vba access-vba ms-access-2010

由于以下代码

,我得到了一个Object _Global失败错误的方法范围

有时候这段代码很有效。我理解它的原因,但不知道如何解决它。如何指定每次选择哪个工作表以使其始终如一地工作。

'Fill Formulas'

Range(columnLetter & "5").Select
Selection.AutoFill Destination:=Range(columnLetter & "5:" & columnLetter & "113"), Type:=xlFillDefault

Range(columnLetter & "143").Select
Selection.AutoFill Destination:=Range(columnLetter & "143:" & columnLetter & "251"), Type:=xlFillDefault

这是我的所有代码

Public Sub AutoUpdateCancels()

    Dim MySheetPath As String
    Dim Xl As Excel.Application
    Dim XlBook As Excel.Workbook
    Dim XlSheet As Excel.Worksheet
    Dim formattedDate As String
    Dim lngRow As Long, intCol As Integer, db As DAO.Database, rst As DAO.Recordset, fld As DAO.Field
    Dim columnLetter As String
    Dim qdf As DAO.QueryDef
    columnLetter = DLookup("[Column]", "[tblColumnIdentifier17]", "[WED] like #" & [Forms]![frmCancelsReporting]![txtCancelsWED] & "#")
    formattedDate = Format(Date, "mm-dd-yyyy")
    MySheetPath = "M:\Chris\Weekly Pulse\Cancel Report\2018\COM\Cancels Report - 2018v2.xlsx"

'Open Excel and the workbook and save a backup
    Set Xl = CreateObject("Excel.Application")
    Set XlBook = Xl.Workbooks.Open(MySheetPath, True)
    Xl.Visible = True
    XlBook.Windows(1).Visible = True
    Set XlSheet = XlBook.Worksheets(11)
    'Xl.ActiveWorkbook.SaveAs FileName:="M:\Chris\Weekly Pulse\Cancel Report\Backups\COM Backup 03-12-2018.xlsx"

'Clear Detail'
    Xl.Range("A256:D371").Select
    Xl.Selection.ClearContents


'Starting Row Number'
    lngRow = 256
'Append New Detail'
    Set db = CurrentDb
    Set qdf = db.QueryDefs("qryCancelsReport")
    qdf.Parameters("EndDate").Value = [Forms]![frmCancelsReporting]![txtCancelsWED]
    Set rst = qdf.OpenRecordset()
    Xl.Cells(lngRow, 1).CopyFromRecordset rst


'Fill Formulas'

    Range(columnLetter & "5").Select
    Selection.AutoFill Destination:=Range(columnLetter & "5:" & columnLetter & "113"), Type:=xlFillDefault

    Range(columnLetter & "143").Select
    Selection.AutoFill Destination:=Range(columnLetter & "143:" & columnLetter & "251"), Type:=xlFillDefault



    Set rst = Nothing
    Set db = Nothing
    Set Xl = Nothing
    Set XlBook = Nothing
    Set XlSheet = Nothing
MsgBox ("Make sure to save over original worksheet not as backup")
End Sub

1 个答案:

答案 0 :(得分:1)

您没有引用正确的对象(例如:您使用的是Xl.Range()而不是xlSheet.Range())

避免使用Select / Selection / Activate / ActiveXXX编码模式使用完全限定范围引用(例如使用xlSheet.Range()而不是Range())

请尝试以下代码:

With XlBook.Worksheets(11) ‘reference wanted sheet. From now on and till next ‘End With’ all referenced sheet members (like its Range()) are just a dot (.) away:

    'Clear Detail'
    .Range("A256:D371").ClearContents

    'Starting Row Number'
    lngRow = 256

    'Append New  
    ....

    .Cells(lngRow, 1).CopyFromRecordset rst

    'Fill Formulas'

    .Range(columnLetter & "5").AutoFill Destination:=.Range(columnLetter & "5:" & columnLetter & "113"), Type:=xlFillDefault

    .Range(columnLetter & "143").AutoFill Destination:=.Range(columnLetter & "143:" & columnLetter & "251"), Type:=xlFillDefault

End With