Sub Import()
'
' Import Macro
'
Dim FilePath As String
FilePath = "TEXT;C:\directory\" & Cells(1, 1).Value
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:=FilePath, Destination:=Range("$A$1"))
.Name = "Book1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
This will create a new sheet with the data when you put the file (book3.csv for instance) in sheet1 A1; you change C:\directory to the directory in which your input exists and you can change the range of output in line 5 "Range"
Additionally, line 3 "Cells(1,1) indicates the input cell, so you can change that from Sheet1 A1 if you'd like. You need to indicate the file extension in the reference cell otherwise it won't know what file to import. Just run this macro and you'll get a new sheet with the target data. If you need a new workbook or for it to insert it into an existing sheet, let me know.
Also, if you look at the properties this is a comma delimited file, you can change that, or just record your own macro (developer tab -> record macro) and insert the FilePath type commands at the top and change the connection command
Data>From text>...and configured my CSV and all. What I want is to be able to change just the path of the file of the already existing datasource. – gregseth Feb 3 at 7:58data -> existing connections -> select connection -> right click edit connection properties -> definition tab -> browseThis would enable you to change it, are you looking to automate that process such that you can type in which one you want into say, cell A1 and it would run the macro in reference to what you entered? – Raystafarian Feb 3 at 11:45