Importing a CSV file with ws.QueryTables.Add

Cazforshort

New Member
Joined
Dec 28, 2015
Messages
1
Hello, Im having trouble importing a csv file. The first row works fine, but the rest are all wonky. Here is a sample of the first and second row from the csv:




"Order # ","Purchased From (Store)","Purchased On","Bill to Name","Ship to Name","G.T. (Base)","G.T. (Purchased)",Status
202366,"RaD.com
Ra D
Ra D Default View
","Dec 26, 2015 12:07:25 PM","John Smith","John Smith",$70.00,$70.00,Pending




The order number goes into the correct cell, but it splits the RaD.com Ra D and Ra D Default View onto different rows in different cells. The date also gets cut into two cells as [,Dec 19][ 2015 12:07:25 PM"]


Heres how its supposed to look.

[TABLE="width: 500"]
<tbody>[TR]
[TD]Order #[/TD]
[TD]Purchased From (Store)[/TD]
[TD]Purchased On[/TD]
[TD]Bill to Name[/TD]
[TD]Ship to Name[/TD]
[TD]G.T. (Base)[/TD]
[TD]G.T. (Purchased)[/TD]
[TD]Status[/TD]
[/TR]
[TR]
[TD]202366[/TD]
[TD]RaD.com
Ra D
Ra D Default View[/TD]
[TD]Dec 26, 2015 12:07:25 PM[/TD]
[TD]John Smith[/TD]
[TD]John Smith[/TD]
[TD]$70.00[/TD]
[TD]$70.00[/TD]
[TD]Pending[/TD]
[/TR]
</tbody>[/TABLE]


Heres my code:
Code:
Sub ImportCSV(fname)
Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count))
ws.Name = "temp" & Worksheets.Count + 1


With ws.QueryTables.Add( _
        Connection:="TEXT;" & fname, _
        Destination:=Range("A1"))
    .Name = "Temp" & Worksheets.Count + 1
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = xlMacintosh
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .Refresh BackgroundQuery:=False
    '.UseListObject = False
End With
End Sub


The RaD.com cell doesn't have to be formatted perfectly, it just all needs to be in one cell. I cant change the way the text file is formatted because there are thousands of them.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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