vba Range.Find using late binding returning null

430 views Asked by At

I have a MS Access(2007) application that needs to update an excel spread sheet at one point.

My users have different workstations with different versions of MS office so i am using late-binding to manipulate the spread sheet.

Before i realized i had to use late binding I wrote all the code and it worked fine, after changing it to late-binding the find function is returning null even for data that worked before i changed it.

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

Why is find returning null now?
Am I doing something wrong with the 'late-binding' or is the problem elsewhere?

0

There are 0 answers