hi guys
i would like to make a macro for importing a file to my workbook. basically, theres a folder with thousands of *.txt files in it, and i want to be able to write
the name of the file i need into a cell.
the folder is always: c:\import\
and i take "example123.txt" as an example of a file id like to import
with the macro recording function and my smallish vba knowledge, i got so far:
Sub importtxtfile()
'
' importtxtfile Macro
'
Dim FileName As String
FileName = Worksheets("Sheet1").Range("J6").Value
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\import\ &filename" _
, Destination:=Range("$A$12"))
.Name = "filename"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveWindow.SmallScroll Down:=-9
End Sub
please look at the red code and tell me whats wrong - i just get an error message saying that it can find the file i found a few guides for this task with google, but i cant get it done alone, maybe its all the " and & and ' and _ in vba code that i am doing wrong, so please be specific. thanks i appreciate it a lot
i would like to make a macro for importing a file to my workbook. basically, theres a folder with thousands of *.txt files in it, and i want to be able to write
the name of the file i need into a cell.
the folder is always: c:\import\
and i take "example123.txt" as an example of a file id like to import
with the macro recording function and my smallish vba knowledge, i got so far:
Sub importtxtfile()
'
' importtxtfile Macro
'
Dim FileName As String
FileName = Worksheets("Sheet1").Range("J6").Value
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\import\ &filename" _
, Destination:=Range("$A$12"))
.Name = "filename"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveWindow.SmallScroll Down:=-9
End Sub
please look at the red code and tell me whats wrong - i just get an error message saying that it can find the file i found a few guides for this task with google, but i cant get it done alone, maybe its all the " and & and ' and _ in vba code that i am doing wrong, so please be specific. thanks i appreciate it a lot