【问题标题】:Export to .csv - How to deal with non-visible quotation marks?导出为 .csv - 如何处理不可见的引号?
【发布时间】:2022-11-21 21:59:06
【问题描述】:

我正在尝试使用 VBA Userform 制作一些 excel 表单,以帮助我们的分析创建有效的 .csv 文件,该文件当前用于将数据添加到数据库。必要的 - 应该只有分号和逗号作为特殊字符。不幸的是,当我尝试将 .xlsm 文件导出到 .csv 时,一切看起来都不错,但是如果我打开文件,例如。 Notepad++ 我看到多余的引号。它看起来像这样:

Excel:

ID GROP ATTR
1234; GRP_A; VAL=abc, DT_CON=2022-10-20; etc.
1235; GRP_B; VAL=abc, DT_CON=2022-10-20; etc.

记事本++: 1234;GRP_A;"VAL=abc, DT_CON=2022-10-20;" 1235;GRP_A;"VAL=abc, DT_CON=2022-10-20;"

我尝试将文件保存为某些格式(包括 xlCSVUTF8 - 但使用此格式时出现错误 1004),使用参数本地:=真.没有什么能像我想要的那样工作。我不知道如何使用 VBA 处理这个问题。

我的代码:

Sub CopyToCSV()
    Dim MyPath As String
    Dim MyFileName As String
        'The path and file names:
    MyPath = "C:\Users\BM73LJ\Documents\" ' ThisWorkbook.Path
     'Makes sure the path name ends with "\":
    If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
        'Copies the sheet to a new workbook:
    Sheets("WYNIKI").Copy
        'The new workbook becomes Activeworkbook:
    MyFileName = MyPath & tb_CorpoKey.Value & "_" & Format(Date, "yyyymmdd") & ".csv"
    ' ActiveWorkbook.WebOptions.Encoding = msoEncodingUTF8
    With ActiveWorkbook
        'Saves the new workbook to given folder / filename:
    .SaveAs Filename:= _
        MyFileName, _
        FileFormat:=xlCSVUTF8
    'Closes the file
    .Close False
End With
End Sub

【问题讨论】:

    标签: excel vba export-to-csv


    【解决方案1】:

    尝试像这样更改您的代码:

        With ActiveWorkbook
            'Saves the new workbook to given folder / filename:
        .SaveAs Filename:= _
            MyFileName, _
            FileFormat:=xlTextPrinter 'THIS IS THE CHANGE
        'Closes the file
        .Close False
    End With
    

    【讨论】:

    • 了解一些关于您提议的更改的背景信息会有所帮助。
    猜你喜欢
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多