VERY long fields in CSV file - QueryTable doesn't import properly, UI Power Query does

Buzzcut

New Member
Joined
Dec 16, 2014
Messages
20
Office Version
  1. 365
Platform
  1. Windows
This is odd. My VBA imports the file OK but the very long fields spill over and overwrite all subsequent columns on that row with blanks:

VBA Code:
ReDim ColumnsType(16383)
    For i = 0 To 16383
        ColumnsType(i) = 2
    Next i
    
    With wbNew.ActiveSheet.QueryTables.Add(Connection:="TEXT;" & FileToOpen, Destination:=wbNew.ActiveSheet.Range("A1"))
        .PreserveFormatting = True
        .RefreshStyle = xlOverwriteCells
        .AdjustColumnWidth = True
        .TextFileParseType = xlDelimited
        .TextFilePlatform = Encoding
        .TextFileTabDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileColumnDataTypes = ColumnsType
        .Refresh BackgroundQuery:=False
    End With

If I go the UI Data-From Text/CSV route then the data imports fine with no spilling. The recorded Macro however isn't that useful.
I don't see any way of uploading my test file, it's only 2 rows and 270KB

This is after going the manual route, all the columns appear normally:

Snap 2023-05-31 at 18.30.18.png


and this is using the VBA above, you can see that E1 (~107k characters) and D2 (~53k characters) have overwritten everything in the columns after.

Snap 2023-05-31 at 18.28.46.png


I tried to skip the fields in the VBA by using the below on my real file. The columns are skipped but the data is still missing, I think it must be importing then deleting.

VBA Code:
.TextFileColumnDataTypes = Array(2, 9, 9, 9, 2......)

Does anyone have an idea how to get the VBA working properly with these long fields?

Thanks...Buzz
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You say the import file is CSV, but your VBA query settings look like you have it set up for tab-delimited, not comma-delimited.
Try this instead (not tested)
VBA Code:
    With wbNew.ActiveSheet.QueryTables.Add(Connection:="TEXT;" & FileToOpen, Destination:=wbNew.ActiveSheet.Range("A1"))
        .PreserveFormatting = True
        .RefreshStyle = xlOverwriteCells
        .AdjustColumnWidth = True
        .TextFileParseType = xlDelimited
        .TextFilePlatform = Encoding
        .TextFileTabDelimiter = False 'True
        .TextFileCommaDelimiter = True 'False
        .TextFileColumnDataTypes = ColumnsType
        .Refresh BackgroundQuery:=False
    End With
 
Upvote 0
You say the import file is CSV, but your VBA query settings look like you have it set up for tab-delimited, not comma-delimited.
Try this instead (not tested)
VBA Code:
    With wbNew.ActiveSheet.QueryTables.Add(Connection:="TEXT;" & FileToOpen, Destination:=wbNew.ActiveSheet.Range("A1"))
        .PreserveFormatting = True
        .RefreshStyle = xlOverwriteCells
        .AdjustColumnWidth = True
        .TextFileParseType = xlDelimited
        .TextFilePlatform = Encoding
        .TextFileTabDelimiter = False 'True
        .TextFileCommaDelimiter = True 'False
        .TextFileColumnDataTypes = ColumnsType
        .Refresh BackgroundQuery:=False
    End With

Yes, the CSV file is actually tab separated, sorry. The VBA works fine for any other file, just not for those which have very long values.
 
Upvote 0
If I go the UI Data-From Text/CSV route then the data imports fine with no spilling. The recorded Macro however isn't that useful.

Why isn't it useful? If it recorded a successful import it sounds like it would be very useful.
 
Upvote 0
Why isn't it useful? If it recorded a successful import it sounds like it would be very useful.
You're right! After reading your comment I looked at it again and it seems doable. My real file has over 100 columns so the query is massive.
I'll have to see if I can push my variables in there somehow though, like filename and Destination. It looks like those might be the only ones.
Thanks for pushing me back to the Macro.

VBA Code:
ActiveWorkbook.Queries.Add Name:="test", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(File.Contents(""C:\Users\U6065108\Downloads\test.csv""),[Delimiter=""   "", Columns=8, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""a"", Int64.Type}, {""b"", type text}, {""c""" & _
        ", type text}, {""d"", type text}, {""e"", type text}, {""f"", type text}, {""g"", type date}, {""h"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=test;Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [test]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "test"
        .Refresh BackgroundQuery:=False
 
Upvote 0
You're right! After reading your comment I looked at it again and it seems doable. My real file has over 100 columns so the query is massive.
I'll have to see if I can push my variables in there somehow though, like filename and Destination. It looks like those might be the only ones.
Thanks for pushing me back to the Macro.

VBA Code:
ActiveWorkbook.Queries.Add Name:="test", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(File.Contents(""C:\Users\U6065108\Downloads\test.csv""),[Delimiter=""   "", Columns=8, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""a"", Int64.Type}, {""b"", type text}, {""c""" & _
        ", type text}, {""d"", type text}, {""e"", type text}, {""f"", type text}, {""g"", type date}, {""h"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=test;Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [test]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "test"
        .Refresh BackgroundQuery:=False

I've got this working now after feeding the "test" name and filename through to make it reusable. One major gotcha seemed to be that the macro didn'tpick up on the tab delimiter properly and listed it as 5 spaces, I had to change it to this. It was giving a constant error on the last line.

VBA Code:
[Delimiter=""" & vbTab & """
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,069
Members
449,092
Latest member
ipruravindra

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