简体   繁体   中英

VBA QueryTables.Add is dropping leading zeros

I am trying to pull data from multiple csv files into my workbook using the QueryTables.Add method. The first three columns have numerical values but I need to pull them over as text so that leading zeros don't drop off. Any ideas on how to do this? Code is below.

Set destCell = Worksheets("Confirm_O").Cells(Rows.Count, "A").End(xlUp)      'CHANGE SHEET NAME
        csvFileName = folder & strFile
        If csvFileName = False Then Exit Sub
        With destCell.Parent.QueryTables.Add(Connection:="TEXT;" & csvFileName, Destination:=destCell)
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileCommaDelimiter = True
            .Refresh BackgroundQuery:=False
        End With
        destCell.Parent.QueryTables(1).Delete

Try adding the xlTextFormat for the column.

With Ws.QueryTables.Add(Connection:="TEXT;" & FileName, _
                        Destination:=Ws.Range("A13"))       ' change to suit
     .TextFileParseType = xlDelimited
     .TextFileCommaDelimiter = True
      'added to retain leading 0s
     .TextFileColumnDataTypes = Array(xlTextFormat)
     .Refresh
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM