2016-09-17 37 views
2

我想用Excel VBA中 我試過很多代碼的簡單的Excel文件轉換成CSV(管道分隔) 文件到CSV文件(管道分隔),但不能得到預期的輸出如何轉換Excel中使用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 
+0

太棒了!你試過什麼代碼?你期望什麼產出?你得到了什麼產出?你能提供樣本數據嗎?儘管有些人可能會在StackOverflow幫助我,但我還沒有遇到一位心靈讀者。請更詳細地編輯您的問題,因爲目前它是一個廣泛問題的定義。 – Kroltan

+0

謝謝#krottan實際的任務是我打開一個excel文件並做一些過濾並將此文件另存爲CSV FILE(管道分隔符)在excel vba –

+0

我明白了。但是你做了什麼,來完成這項任務?發佈代碼,錯誤(如果有的話)以及一些數據和預期輸出,以便我們可以幫助您。嘗試製作[mcve]。 – Kroltan

回答

2

您可以保存一個Excel文件作爲逗號分隔或製表符分隔而不是管道分隔。

以下是如何實現管道分隔的輸出。

Basic示例

只是爲了在這裏顯示的基本面。

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 
0

如果你只是想走出節省紙張作爲管道分隔文件,那麼這應該爲你工作:

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