Data file imports using variables

krissz

Board Regular
Joined
Feb 21, 2010
Messages
95
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.

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.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
After the two rows where you create the variable, add a Message Box to return its value:
Code:
MsgBox CSV_Name
When you run it, does the message box return:
TEXT;J:\ACCOUNTS\Credit Cards\2016_\statement_201604.csv
If not, modify the variable as needed so it does.

Also, do NOT set the .Name property equal to CSV_Name. That is the name of the sheet, and slashes are not allowed in the name.
Just make it something like:
Code:
.Name = "Sheet1"
or
Code:
.Name = "statement"
 
Last edited:
Upvote 0
Joe4
Thanks for you advice. I now have the correct string but get an Run-time error 1004 when using the variable but it works smoothly with the text. Just in case, I have recorded a macro for this action several times.
I am confused.
 
Upvote 0
Joe
I have solved the problem. I was adding extra CHR(34), originally CHR(39) when defining CSV_File.
This is now that part of the code:
'Import CSV file


Code:
    CSV_Name = Range("Master!B3").Value & Range("Master!B17").Value
    CSV_Name = "TEXT" & Chr(59) & CSV_Name & ".CSV"
    
    With ActiveSheet.QueryTables.Add(Connection:=CSV_Name, Destination:=Range("$A$2"))
'        "TEXT;J:\ACCOUNTS\Credit Cards\2016\statement_201604.csv", Destination:=Range("$B$1"))
        .Name = "statement_201604"
        .FieldNames = True
        .RowNumbers = False
       etc

Found the answer at:

excel vba - CSV Import into a spreadsheet fails on QueryTables method - Stack Overflow

Thanks for your help
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top