Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88066

VBA Open Workbook from Macro fails "(path & filename) could not be found. Check the spelling..."

$
0
0

I have the following code, which should simply open a workbook from a master workbook to allow me to paste some information which I will later retrieve from a database. I receive the error in the title every time I try to open a workbook. I have used similar code before with no problem at all. The files are kept on a server away from my desktop, but the path has been correct each time. I've checked over and over again. What could be the culprit?

Sub copyDealerDataToWorkbook(ByVal targetID As Integer)

    Dim mainWB      As Workbook    
    Dim directory   As String
    Dim fn          As String
    Dim aFile       As Excel.Application

    Set aFile = CreateObject("Excel.Application")

    Set mainWB = ActiveWorkbook
    directory = ActiveWorkbook.Path

    'append a "\"
    If InStrRev(directory, "\") < Len(directory) Then
        directory = directory & "\"
    End If

    fn = targetID & ".xls"

    aFile.Application.Visible = True
    aFile.Workbooks.Open directory & fn 'ERROR ON THIS LINE
    aFile.Parent.Windows(1).Visible = True
End Sub

I have also tried the following code...

Sub copyDealerDataToWorkbook(ByVal targetID As Integer)
    Dim foreignWB   As Workbook
    Dim mainWB      As Workbook
    Dim directory   As String
    Dim fn          As String

    Set mainWB = ActiveWorkbook
    directory = ActiveWorkbook.Path

    'append a "\"
    If InStrRev(directory, "\") < Len(directory) Then
        directory = directory & "\"
    End If

    fn = targetID & ".xls"

    Set foreignWB = Workbooks.Open(fileName:=directory & fn) 'ERROR ON THIS LINE
End Sub

Viewing all articles
Browse latest Browse all 88066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>