|
-
Dec 17th, 2009, 05:24 AM
#1
Thread Starter
New Member
[RESOLVED] Open CSV File - Please, Help!
Good Morning everyone,
I'm having trouble importing CSV via macro in Excel 2007.
I found several answers here but none of them solved my problem.
When I open the file or try to import it, the formatting is lost. I found functions to the CSV format but the only one that worked I can not include a variable to read the path of the CSV
This works for me:
Code:
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT; [here goes the path with the file]" _
, Destination:=Range("$A$1"))
.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 = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
But, i can not put a variable after the code "TEXT;" I don't know how to do it.
Someone help me please. I'm going crazy with it. This function format my CSV but I can not point the variable to open the file.
Thank you all for help,
Hugs,
Caio
-
Dec 17th, 2009, 05:39 AM
#2
Thread Starter
New Member
Re: Open CSV File - Please, Help!
I'm sorry people. I resolved my problem
I do this:
Code:
Arquivo = "TEXT;" + sLinha
With ActiveSheet.QueryTables.Add(Connection:= _
Arquivo _
, Destination:=Range("$A$1"))
.FieldNames = True
.RowNumbers = False
Tks everyone.
Hugs,
Caio
-
Dec 17th, 2009, 05:41 AM
#3
Re: [RESOLVED] Open CSV File - Please, Help!
you can try this
vb Code:
mypath = "c:\temp\myfile.xls" With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT; " & mypath _ , Destination:=Range("$A$1"))
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|