Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何使用VBA保存分号分隔的csv文件?_Excel_Vba - Fatal编程技术网

Excel 如何使用VBA保存分号分隔的csv文件?

Excel 如何使用VBA保存分号分隔的csv文件?,excel,vba,Excel,Vba,我将数据复制到电子表格中,使用VBA对其进行格式化,然后将该表格保存到CSV文件中 我使用以下代码: ws.SaveAs Filename:=filestr, Fileformat:=xlCSV ws是我保存的工作表 这给了我一个逗号分隔的CSV文件 我想将该工作表保存到以分号分隔的文件中 我发现: 转到开始>设置>区域和语言选项 单击“自定义”按钮 分号(;)中列表分隔符类型旁边 我按照上述程序将代码更改为: ws.SaveAs Filename:=filestr, Fileformat:=

我将数据复制到电子表格中,使用VBA对其进行格式化,然后将该表格保存到CSV文件中

我使用以下代码:

ws.SaveAs Filename:=filestr, Fileformat:=xlCSV
ws是我保存的工作表

这给了我一个逗号分隔的CSV文件

我想将该工作表保存到以分号分隔的文件中

我发现:

  • 转到开始>设置>区域和语言选项
  • 单击“自定义”按钮
  • 分号(;)中列表分隔符类型旁边
  • 我按照上述程序将代码更改为:

    ws.SaveAs Filename:=filestr, Fileformat:=xlCSV, Local:=True
    
    我仍然得到一个逗号分隔的CSV文件作为输出

    我使用的是Excel 2003,我的操作系统是Windows XP。

    只需使用以下代码: ActiveWorkbook.SaveAs“My File.csv”,xlCSV,Local:=True


    (不要使用:Filename:=)

    我刚刚检查了这个,因为我遇到了同样的问题。在这种情况下,Filename没有任何功能

    这就是我的工作原理:

    With ActiveWorkbook
        .SaveAs Filename:="My File.csv", FileFormat:=xlCSV, Local:=True
        .Close False
    End With
    

    在区域设置中->;使用Close时,您必须使用
    False

    在成功构建之后使用vbs脚本:

    .SaveAs文件名,6,0,0,0,0,0,0,0,0,0,1

    其中参数为:

    Object Filename,
    Object FileFormat,
    Object Password,
    Object WriteResPassword,
    Object ReadOnlyRecommended,
    Object CreateBackup,
    XlSaveAsAccessMode AccessMode,
    Object ConflictResolution,
    Object AddToMru,
    Object TextCodepage,
    Object TextVisualLayout,
    Object Local
    
    SourceLink:

    “SaveAs”函数中的最后一个“1”等于Local=True


    此外,分号必须定义为操作系统区域设置中的列表分隔符(请参见上面的答案)

    无需声明所有这些变量,只需在SaveAs方法的末尾添加local:=true,如下所示:

    ActiveWorkbook.SaveAs Filename:="C:/Path/TryMe.csv", FileFormat:=xlCSV, Local:=True
    

    使用xlSCVMSDOS读取xlCSV

    ActiveWorkbook.SaveAs Filename:="my File.csv", FileFormat:= xlCSVMSDOS, Local:=True
    

    这对我来说很管用

    我遇到了同样的问题,在考虑尝试使用VBA代码和内核调用更改区域设置中的“行分隔符”后,我决定这会更痛苦,因此我找到了一些使用Scripting.FileSystemObject来满足我需要的示例。

    下面的代码将采用现有的csv文件,并将所有逗号替换为波浪号“~”字符

    Private Sub commaReplace()
    
        Dim objFSO
        Dim filePath
        Dim migratorFileName
        Dim strFullPath1
        Dim strFullPath2
        Const ForReading = 1
        'define a TextStream object
        Dim objTS
        Dim strContents As String
    
        'note, my code actually uses the below commented out filepath
        'as the location of the workbook can be arbitrary, e.g.
        'Worksheets("FilePath").[A2:A2].Value is determined when workbook
        'is opened
        'filePath = Worksheets("FilePath").[A2:A2].Value
        filePath = "C:\Temp\"
    
        'our original file that we've exported as csv file in another section of code
        migratorFileName = "MigratorInput.csv"
        strFullPath1 = filePath + migratorFileName
    
        'the path and file name we want to save to, tilde separated vs. comma
        migratorFileName = "MigratorInput.tilde.csv"
        strFullPath2 = filePath + migratorFileName
    
        'read everything from the csv file, replacing comma with tilde
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objTS = objFSO.OpenTextFile(strFullPath1, ForReading)
        strContents = objTS.ReadAll
        strContents = Replace(strContents, ",", "~")
        objTS.Close
    
        'write everything out to another file, note, this could just overwrite
        'the original file if you pass the optional overwrite flag
        Set objTS = objFSO.CreateTextFile(strFullPath2)
        objTS.Write strContents
        objTS.Close
    
    End Sub
    
    然后,您可以从正在创建csv文件的子程序中调用commaReplace子程序


    希望它能帮助别人

    这里有一种手动操作的方法;将分隔符替换为“;”,将范围替换为ActiveSheet.UsedRange非常感谢您的建议。但是我在尝试运行您的代码时遇到了一个问题。我的电子表格中有一些值,例如
    1.0000000
    ,当它通过
    CStr(c.value)
    转换为字符串时,它会自动四舍五入到
    1
    。你能建议怎么处理吗?谢谢:)若要继续格式化,请使用
    c.text替换
    CStr(c.Value)
    非常感谢您的帮助:)效果非常好。。。但有必要设置“.Close False”!我正在使用Excel 2007,仅在调用“.Close False”后使用“;”作为列表分隔符保存我的CSV。谢谢使用“Filename:=”或“not”都不会改变任何东西在我看来,这对上面的答案没有任何价值。数据中的逗号会发生什么变化?