I am trying to set up an application which allows a user to import a text file (say a credit card statement, expenses, etc) into excel, work on it (cost codes, tax, etc) & then output a verified text file which can be used to import the data in the accounts system.
If I hard code the connection string, it works perfectly.
However, when I try to use variables for the connection string, it fails
I have been googling but can find little apart from SQL connections. It's probably very simple, but I cannot resolve it.
If I hard code the connection string, it works perfectly.
Code:
With ActiveSheet.QueryTables.Add(Connection:=_
"TEXT;J:\ACCOUNTS\Credit Cards\2016_\statement_201604.csv", _
Destination:=Range ("$B$1"))
.Name = "statement_201604"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(4, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
However, when I try to use variables for the connection string, it fails
Code:
CSV_Name = Range("Master!B3").Value & Range("Master!B17").Value
CSV_Name = Chr(39) & "TEXT;" & CSV_Name & ".CSV" & Chr(39)
With ActiveSheet.QueryTables.Add(Connection:=CSV_Name, Destination:=Range("$A$2"))
.Name =CSV_Name
.FieldNames = True
.RowNumbers = False
etc
I have been googling but can find little apart from SQL connections. It's probably very simple, but I cannot resolve it.