【问题标题】:Import CSV (UTF-8) to Excel without formatting将 CSV (UTF-8) 导入 Excel 而不进行格式化
【发布时间】:2019-10-17 06:49:14
【问题描述】:

我有将数据从 CSV 文件粘贴到工作表的代码。 Everyhting 工作正常,但我目前的解决方案面临一些问题。

有没有办法在不格式化的情况下粘贴数据?现在 Excel 正在格式化例如 -Name=-Name 所以有 #Name? 错误而不是值。它还将215067910018 格式化为2,15068E+11

我的 CSV 字符串如下所示:

123456,"Word1","Word2","-Word3",,"Word4","Word5","00076",,"Word7","Word8"

在 Excel 单元格中运行代码 "-Word3" 后看起来像 #Name?。我也得到76而不是00076

这是我当前的代码:

Sub GetCustomers()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets.Add(After:= _
             ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    ws.Name = "Sheet1"

    Dim strText As String

    Dim file As String
    file = "L:\15\186507.CSV

    ' read utf-8 file to strText variable
   With CreateObject("ADODB.Stream")
        .Open
        .Type = 1  ' Private Const adTypeBinary = 1
        .LoadFromFile file
        .Type = 2  ' Private Const adTypeText = 2
        .Charset = "utf-8"
        strText = .ReadText(-1)  ' Private Const adReadAll = -1
    End With

    ' parse strText data to a sheet
    intRow = 1
    For Each strLine In Split(strText, Chr(10))
        If strLine <> "" Then
            With ws
                .Cells(intRow, 1) = strLine
                .Cells(intRow, 1).TextToColumns Destination:=Cells(intRow, 1), DataType:=xlDelimited, _
                    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                    Semicolon:=False, Comma:=True, Space:=False, Other:=False
            End With

            intRow = intRow + 1
        End If
    Next strLine

End Sub

编辑:

我可以在手动将文本转换为列时做到这一点。 VBA不应该也可以吗?我的意思是设置列数据格式,而.TextToColumns

【问题讨论】:

  • TextToColumns 方法指定TextQualifier=xlDoubleQuote。双引号中的 -Name 之类的东西吗?如果有,这个问题可能会消失吗?
  • @CindyMeister 它们用双引号括起来,例如“-Word3”。但是问题还是出现了。
  • 如果您使用 Power Query 导入(在 Excel 2010+ 中可用),它会尊重双引号并将 "-Wordn" 作为文本字符串导入。
  • @RonRosenfeld 我正在考虑它,但我不确定如果我的 CSV 不断更新并且有新行出现,它将如何工作。每次打开工作簿时都会更新表格。我想创建 VBS 来每天运行这个应用程序。因此,它将 2 个 CSV 文件导入到一个工作簿,将它们连接在一起,然后将它们导出到我们的文件管理系统。
  • 刷新选项列在查询属性对话框窗口中。在文件打开是选项之一。您也可以使用计时器。如果需要其他触发器,还可以编写 VBA 事件代码。

标签: excel vba


【解决方案1】:

您可以使用TextToColumns的参数FieldInfo来指定数据类型。例如,要为第 1 列和第 4 列指定文本,您将具有以下...

.Cells(intRow, 1).TextToColumns _
            Destination:=.Cells(intRow, 1), _
            DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, _
            Tab:=False, _
            Semicolon:=False, _
            Comma:=True, _
            Space:=False, _
            Other:=False, _
            FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1), Array(4, 2), Array(5, 1), _
                Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1))

对于数组中的每一对,第一个数字表示列号,第二个数字表示数据类型(1 = 常规;2 = 文本;等等......)。更多详情请关注here

【讨论】:

    【解决方案2】:

    我尝试将目标单元格的格式设置为Text(有时适用于例如复制操作),但这也无济于事.....

    不是一个优雅的解决方案,但在测试时对我有用,是纠正之后由 .TextToColumns 操作引入的任何公式。见下文:

    Dim cl As Range, rg As Range
    

    作为strLineLoop 的一部分

    On Error Resume Next
    Set rg = .Rows(1).SpecialCells(xlFormulas)
    If Err.Number = 0 Then                                      'Test if SpecialCells retuned something
        For Each cl In .Rows(intRow).SpecialCells(xlFormulas)
            cl.Value = Mid(cl.Formula, 2)                       'Get rid of the erreanously introduced "="
        Next cl
    End If
    On Error GoTo 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 2013-10-06
      • 2011-04-17
      • 2014-02-03
      • 1970-01-01
      相关资源
      最近更新 更多