简体   繁体   English

VBA QueryTables.Add 正在删除前导零

[英]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.我正在尝试使用 QueryTables.Add 方法将多个 csv 文件中的数据提取到我的工作簿中。 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.尝试为列添加 xlTextFormat。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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