VBA - Import CSV - Delimiter issues

Fishboy

Well-known Member
Joined
Feb 13, 2015
Messages
4,267
Hi guys,


I have been working on a VBA macro to import a CSV file into Excel 2010. On the whole it has been successful with most CSV files however with one particular batch the first "column" looks like this


item:45758:0:0:0:0:0:0


As such Excel is insisting on breaking the data up using the colons as delimiters despite this all being "one entry for one column". The rest of the CSV imports fine using commas as delimiters as intended but where this one column becomes eight columns, it is messing up all of the headers.


I have tried manually recording a macro to ensure that either A) colon is NOT used as a delimiter, or B) the entire first colum is not imported, but nothing seems to stop this from happening.


Can anyone see what I can do to prevebnt this in the code I am using below please?




Code:
' Sales_Import Macro
'


'
    Sheets("Item Lookups").Select
    Sheets("Raw Sales Data").Visible = True
    Sheets("Raw Sales Data").Select
    Cells.Select
    Selection.Delete Shift:=xlUp
    Range("A1").Select
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\DBAH\FILENAME.csv", Destination:=Range( _
        "$A$1"))
        .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 = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(9, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    Sheets("Raw Sales Data").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Item Lookups").Select
End Sub

Many thanks!
 

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.
There are some your could try changing like .TextFileTextQualifier = xlTextQualifierDoubleQuote.

Otherwise, try obfuscating text file data and posting or post a sample text file to a free site like dropbox.
 
Upvote 0
Since column 1 is being imported as a Date value, the imported value must be in the consistent format.
You may need to import the values as Text first and then convert to your date values.
Or better yet read the text in the Macro and validate each one before actually recording to the sheet.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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