View Single Post
 
Old 12-26-2020, 10:07 PM
SoMany SoMany is offline Windows 7 64bit Office 2016
Advanced Beginner
 
Join Date: Oct 2016
Posts: 51
SoMany is on a distinguished road
Default

Quote:
Originally Posted by Purfleet View Post
A bit basic, but this should do what you want.

The file location needs to be in cell B1 and the file name in B2

Code:
Sub ImportFile()

    Dim fLocation As String
    Dim fName As String
    Dim CurrentfName As String
    
    CurrentfName = ActiveWorkbook.Name
    fLocation = ActiveSheet.Range("B1")
    fName = ActiveSheet.Range("b2")
    
    If Len(Dir(fLocation & fName, vbDirectory)) = 0 Then
        MsgBox Title:="Error", Prompt:="Cant find file"
        Exit Sub
    End If
    
    If Right(fLocation, 1) <> "/" Then
        fLocation = fLocation & "/"
    End If
    
    Workbooks.Open (fLocation & fName)
    
    ActiveSheet.Copy Workbooks(CurrentfName).Sheets(1)
    
    Workbooks(fName).Close False

End Sub
I got an error message saying "Cant find file".
Reply With Quote