Hi every body
i have the following code to get excel sheet name and last row number it work fine with no problem but when i go to next record in my form and press the button agin it produce error 91 "object variable or with block not set"
please review the code :
i have the following code to get excel sheet name and last row number it work fine with no problem but when i go to next record in my form and press the button agin it produce error 91 "object variable or with block not set"
please review the code :
Code:
‘ this code to get sheet name and last row in column A
‘General declaration
Dim LastRow As Long
Dim SheetName As String
'Declare Excel file
Dim strPath As String
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWsh As Object
Dim MyRange As Range
'open Excel file
strPath = mypath
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(strPath)
SheetName = ActiveSheet.Name
Me!Sheet_Name = SheetName
Set xlWsh = xlWBk.Worksheets(SheetName)
Set MyRange = xlWsh.Range("A" & "1")
LastRow = Cells(65536, ("A")).End(xlUp).Row
Me!Last_Row = LastRow
' close Excel
' ===================================
If Not MyRange Is Nothing Then
Set MyRange = Nothing
End If
If Not xlWsh Is Nothing Then
xlWsh.Close
Set xlWsh = Nothing
End If
If Not xlWBk Is Nothing Then
xlWBk.Close True
Set xlWBk = Nothing
End If
If Not ApXL Is Nothing Then
ApXL.Quit
Set ApXL = Nothing
End If