Open Excel Sheet , Runtime Error 91
Hello,
I am Trying to open a excel sheet ,the name of excel sheet i am assigning in the text box, i need to open the sheet " Employee Attendence" and write in the cells(1,1) ="hai"
i have written the below code
Code:
Private Sub Command1_Click(Index As Integer)
Dim vari As String
Dim uppxl As Excel.Application
Dim upxwb As Excel.Workbook
Dim upxws As Excel.Worksheet
vari = eempid.Text
'error is here
Set upxwb = uppxl.Workbooks.Open(App.Path & "\" & eempid.Text & ".xlsx")
'error is here
Set upxws = upxwb.Worksheets("Employee Attendence")
With upxws
.Cells(1, 1).Value = "hai"
End With
upxwb.Close SaveChanges:=True
Set upxwb = Nothing
uppxl.Quit
Set uppxl = Nothing
End Sub
i am getting a runtime error 91 ,object variable or With block variable not set.
kindly help
Re: Open Excel Sheet , Runtime Error 91
It's one of those self-explanatory errors: you need to Set object before using it:
Set uppxl = New Excel.Application
Re: Open Excel Sheet , Runtime Error 91
Also be careful how you use App.Path because sometimes it may already end with a backslash. So do this instead.
Code:
Dim AppPath As String
AppPath = App.Path
If Right$(AppPath, 1) <> "\" Then
AppPath = AppPath + "\"
End If
Set upxwb = uppxl.Workbooks.Open(AppPath & eempid.Text & ".xlsx")