【问题标题】:VBA CSV import: long numbers are not written outVBA CSV 导入:长数字未写出
【发布时间】:2022-01-06 18:05:17
【问题描述】:

我有一个 CSV 文件,我想使用 QueryTable 将其显示在我的电子表格中。

因为我的数字很长,我想将单元格格式化为文本(因为最后一位数字变成了“0”)。

我尝试预先格式化单元格并将“PreserveFormatting”设置为 True,但这仍然给我留下一个“E+16”数字,如果我点击它,最后 2 位数字将替换为“0”。

你知道一种方法,将完整的数字作为文本写入单元格吗?

这是我的 CSV:


    1,Name1,Surname1,26778942223654789,,,,,,,,,,,
    2,Name2,Surname2,31678678797467889,,,,,,,,,,,
    3,Name3,Surname3,31657894475133527,,,,,,,,,,,
    4,Name4,Surname4,35448512368896137,,,,,,,,,,,

这是我的代码:


    Sub Personen_importieren()

    import_path = Range("A2").Text

    If import_path <> False Then
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & import_path,_ Destination:=ActiveSheet.Range("A9"))
             .TextFileParseType = xlDelimited
             .TextFileCommaDelimiter = True
             .PreserveFormatting = True
             .AdjustColumnWidth = False
             .Refresh
        End With
    End If

    End Sub

【问题讨论】:

  • 您需要在查询时指定列类型,而不是之后。我建议您在使用旧版导入向导时录制宏,并确保将具有长数字的列的数据类型设置为文本。然后调整该记录以将文件路径替换为 Range 的内容。

标签: excel vba csv import long-integer


【解决方案1】:

感谢您的回答

按照这两个提示,我录制了一个宏,我根据需要在其中导入了 CSV 文件。

这向我展示了正确的属性“.TextFileColumnDataTypes”

之后,我必须删除所有连接,否则打开时工作表出错。

现在这是我的代码:

Sub Personen_importieren()

    import_path = Range("A2").Text

    If import_path <> False Then
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & import_path, Destination:=ActiveSheet.Range("A9"))
             .TextFileParseType = xlDelimited
             .TextFileCommaDelimiter = True
             .PreserveFormatting = True
             .RefreshOnFileOpen = False
             .AdjustColumnWidth = False
             .TextFileColumnDataTypes = Array(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
             .Refresh
        End With
        
        Dim cn
        For Each cn In ThisWorkbook.Connections
            cn.Delete
        Next cn
        For Each cn In ActiveSheet.QueryTables
            cn.Delete
        Next cn
    
    End If

    End Sub

标题

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 2015-08-20
    • 2011-04-16
    • 2014-06-06
    • 1970-01-01
    • 2013-10-17
    • 2018-01-11
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多