Reputation: 139
I'm trying to run a macro, but when referencing a excel file I have set up already I am getting a run time error and highlights which references my path. I am almost certain that this path is correct because I went to the properties of the excel file and copied the location
Set xlWB = xlApp.Workbooks.Open(strPath)
' Get Excel set up
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
Debug.Print strPath = enviro & "\Documents\multipliers.xls"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Test1")
' Process the message record
Upvotes: 0
Views: 508
Reputation: 7107
Private Sub doingstuff()
Dim xlWB As Workbook
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
strPath = Environ("USERPROFILE") & "\Desktop" & "\" & "Book2" & ".xlsm"
Set xlWB = Workbooks.Open(strPath)
End Sub
this works fine for me.
Upvotes: 0
Reputation: 4514
Your file is a .xlsx
file, not .xls
as in your code. Change the following line:
strPath = enviro & "\Documents\multipliers.xls"
to:
strPath = enviro & "\Documents\multipliers.xlsx"
Upvotes: 2