vba Range.Find使用后期绑定返回null

时间:2017-08-31 22:55:31

标签: excel vba ms-access access-vba late-binding

我有一个MS Access(2007)应用程序需要一次更新excel电子表格。

我的用户使用不同版本的MS Office有不同的工作站,因此我使用后期绑定来操作电子表格。

在我意识到我必须使用后期绑定之前,我编写了所有代码并且它工作正常,在将其更改为后期绑定之后,即使对于在我更改之前工作的数据,返回null也是如此。

Private Sub SaveRejectsToExcel()
    Dim ExcelApp As Object
    Dim wbk As Object

On Error Resume Next
    Set ExcelApp = GetObject(, "Excel.Application")

If Err.Number <> 0 Then
    Err.Clear
    Set ExcelApp = CreateObject("Excel.Application")

End If

Set wbk = ExcelApp.Workbooks.Open(CurDir & "\TestRejectsSS.xlsx")
ExcelApp.Visible = True
With wbk

    Me.subForm.Form.Recordset.MoveFirst
    Do While Me.subForm.Form.Recordset.EOF = False
        Dim machine, tool, Rejects
        machine = Me.subForm.Form.getMachineNo
        tool = Me.subForm.Form.getMachineTool
        Rejects = Nz(DSum("Rejects", "SMIwDate", "SMI_Machine_No = '" & machine & "' AND Tool_ID = '" & tool & "' AND [Day] = #" & Format(Me.shDay, "mm/dd/yyyy") & "#"), 0)
        With .worksheets(machine)
            Dim rowNo, colNo
            Debug.Print .Cells(1, 1)    'to check its on the right sheet
            colNo = .Cells.Find(What:=Format(Me.shDay, "dd-mmm"), After:=.Cells(1, 1), LookIn:=xlvalues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
'i have that date format because thats how it displays on the spread sheet, i have also tried mm/dd/yyyy and dd/mm/yyyy
            rowNo = .Cells.Find(What:=tool, After:=.Cells(1, 1), LookIn:=xlvalues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
            Debug.Print .Cells(1, 2)
            .Cells(rowNo, colNo).Value = Rejects
        End With

        Me.subForm.Form.Recordset.MoveNext
    Loop

End With
ExcelApp.DisplayAlerts = False
wbk.Close savechanges:=True
ExcelApp.DisplayAlerts = True
ExcelApp.Quit
End Sub

为什么现在找到返回null?
我是否对“后期绑定”做错了什么?或其他地方的问题?

0 个答案:

没有答案