【问题标题】:how to convert excel file to CSV file (pipe delimited) using excel vba如何使用 excel vba 将 excel 文件转换为 CSV 文件(管道分隔)
【发布时间】:2018-01-22 18:12:07
【问题描述】:

我想将简单的 excel 文件转换为 CSV(管道分隔) 使用 excel vba 我尝试了很多代码,但无法获得预期的输出

following code I tried

      Sub mergeFiles()
        Dim xlwkbInput1  As Workbook
        Dim xlshtInput1  As Worksheet
        Dim xlwbfinalrpt As Workbook
        Dim xlshtfinalrpt As Worksheet

        Dim rcount1 As Long

        Dim xlwkbInput2  As Workbook
        Dim xlshtInput2 As Worksheet
        Dim xlapp As Excel.Application


        Set xlapp = New Excel.Application
        xlapp.Visible = True               

           Set xlwkbInput1 = xlapp.Workbooks.Open(ActiveWorkbook.Path & "\Output\Operative_CashFlow_Report.xlsx")
           Set xlwkbInput2 = xlapp.Workbooks.Open(ActiveWorkbook.Path & "\Output\Collection_CashFlow_Report.xlsx")

           xlwkbInput2.Sheets("Sheet1").Activate
           xlwkbInput2.ActiveSheet.UsedRange.Copy

           xlwkbInput1.Sheets("Sheet1").Activate
           rcount = xlwkbInput1.ActiveSheet.UsedRange.Rows.Count

           xlwkbInput1.Sheets("Sheet1").Range("A" & CStr(rcount + 1)).PasteSpecial

           xlwkbInput1.UsedRange("$A$1:$I$274").AutoFilter Field:=1, Criteria1:=Array( _
           "LIC106", "LIC107", "LIC134", "LIC138", "="), Operator:=xlFilterValues
           xlwkbInput1.UsedRange.Delete             
           xlwkbInput1.SaveAs ActiveWorkbook.Path & "\Output\final_report.xlsx"              
           Set xlwbfinalrpt = xlapp.Workbooks.Open(ActiveWorkbook.Path & "\Output\final_report.xlsx")       
           xlwbfinalrpt.Sheet("Sheet1").Activate

            xlwbfinalrpt.SaveAs ActiveWorkbook.Path & "\Output\final_report.xlsx"      

    xlwbfinalrptwb = Workbooks.Open (ActiveWorkbook.Path & "\Output\final_report.xlsx" 

         xlwbfinalrptwb .SaveAs fileName:=ActiveWorkbook.Path & "\Output\final_report.xlsx"
    , FileFormat:=xlCSV, CreateBackup:=False  

' 这里我正在将 excel 转换为 CSV 文件

End Sub

【问题讨论】:

  • 太棒了!你试过什么代码?你期待什么输出?你得到了什么输出?你能提供样本数据吗?尽管 StackOverflow 上有些人可能会提供帮助,但我还没有遇到过读心者。请使用更多详细信息编辑您的问题,因为目前它是一个广泛问题的定义。
  • 感谢#krottan,实际任务是我打开一个excel文件并进行一些过滤并将此文件保存为excel vba中的CSV文件(管道分隔)
  • 我明白了。但是您尝试过什么来完成此任务?发布代码、错误(如果有)以及一些数据和预期输出,以便我们为您提供帮助。尝试制作minimal reproducible example
  • 我试过这段代码
  • 使用问题正文左下角的编辑按钮向问题添加信息,以便获得更好的格式。 (其实我没看懂你指的是什么代码)

标签: excel vba


【解决方案1】:

您可以将 Excel 文件保存为逗号分隔或制表符分隔但不能以竖线分隔。

这是实现管道分隔导出的方法。

基本示例

只是在这里展示基本原理。

Sub Writing_to_a_text_file()
    Dim N As Integer
    Dim FileName As String

    'Define where to save the output file.
    FileName = Environ("USERPROFILE") & "\Desktop\" & "Sample1.csv" 

    'Get a free file number
    N = FreeFile

    Open FileName For Output As #N
        '"Print" print data into the file. Another method is "Write". 
        'Both do the same job but behave slightly differently. Try Google it.
        Print #N, "This is a test" 
        Print #N, "Writing another line here" 
        Print #N, Join(Array("Pipe", "delimited", "line", "here"), "|") 
        Print #N, vbNullString '<- this create an empty line
    Close N
End Sub

以管道分隔格式将一系列数据导出到文本文件中

Sub ExportToTextFile()
'Export range("A1:E10") data to a text file in pipe delimited format.
    Dim N As Integer
    Dim FileName As String
    Dim R As Long, C As Long, DataLine As String

    FileName = Environ("USERPROFILE") & "\Desktop\" & "TextOutput.csv" 

    N = FreeFile

    Open FileName For Output As #N
        For R = 1 To 10
            DataLine = vbNullString
            For C = 1 To 5
                DataLine = DataLine & "|" & Cells(R, C).Value2
            Next C
            DataLine = Right(DataLine, Len(DataLine) - 1)
            Print #N, DataLine
        Next R
    Close N
End Sub

【讨论】:

    【解决方案2】:

    如果您只想将工作表另存为管道分隔文件,那么这应该适合您:

    Sub DelimFile()
    
    Open "C:\output.txt" For Output As 1 'Change this path
    
    rowno = 1
    colcount = Application.CountA(ActiveSheet.Rows(1))
    While activesheet.Cells(rowno, 1) <> ""
        dataout = ""
        For c = 1 To colcount
            If c <> colcount Then
                dataout = dataout & """" & Trim(activesheet.Cells(rowno, c)) & """|"
            Else
                dataout = dataout & """" & Trim(activesheet.Cells(rowno, c)) & """"
            End If
        Next c
        Print #1, dataout
        rowno = rowno + 1
    Wend
    Close #1
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 2023-03-15
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多