2016-11-09 70 views
0

我使用用戶The_Barman從一個Excel文檔導出特定細胞的CSV文件提供瞭解決方案CSV文件:導出Excel數據使用Excel文件名

Sub WriteCSVFile() 

Dim My_filenumber As Integer 
Dim logSTR As String 

My_filenumber = FreeFile 

logSTR = logSTR & Cells(18, "C").Value & " , " 
logSTR = logSTR & Cells(19, "C").Value & " , " 
logSTR = logSTR & Cells(20, "C").Value & " , " 
logSTR = logSTR & Cells(21, "C").Value & " , " 
logSTR = logSTR & Cells(27, "C").Value & " , " 
logSTR = logSTR & Cells(28, "C").Value & " , " 
logSTR = logSTR & Cells(29, "C").Value & " , " 
logSTR = logSTR & Cells(30, "C").Value & " , " 
logSTR = logSTR & Cells(31, "C").Value & " , " 
logSTR = logSTR & Cells(32, "C").Value & " , " 

Open "C:\temp\Filename.csv" For Append As #My_filenumber 
    Print #My_filenumber, logSTR 
Close #My_filenumber 

End Sub 

是否會我想知道是可能的,以及如何讓CSV文件名引用源文件的VBA腳本的文件名,而不是每次都要手動輸入代碼。

即,具有 「C:\ TEMP \ Filename.csv」 另存爲 「C:\ TEMP \ ExcelSpreadsheet.csv」,而無需手動輸入 「ExcelSpreadsheet」 文件名每次運行時間:

Open "C:\temp\Filename.csv" For Append As #My_filenumber Print #My_filenumber, logSTR Close #My_filenumber

謝謝你的任何援助,您可以提供

回答

0

試試這個:

Sub WriteCSVFile() 

    Dim My_filenumber As Integer 
    Dim logSTR As String 

    My_filenumber = FreeFile 

    logSTR = logSTR & Cells(18, "C").Value & " , " 
    logSTR = logSTR & Cells(19, "C").Value & " , " 
    logSTR = logSTR & Cells(20, "C").Value & " , " 
    logSTR = logSTR & Cells(21, "C").Value & " , " 
    logSTR = logSTR & Cells(27, "C").Value & " , " 
    logSTR = logSTR & Cells(28, "C").Value & " , " 
    logSTR = logSTR & Cells(29, "C").Value & " , " 
    logSTR = logSTR & Cells(30, "C").Value & " , " 
    logSTR = logSTR & Cells(31, "C").Value & " , " 
    logSTR = logSTR & Cells(32, "C").Value & " , " 

    Open "C:\temp\" & ThisWorkbook.Name & ".csv" For Append As #My_filenumber 
      Print #My_filenumber, logSTR 
    Close #My_filenumber 

End Sub 

但是,你知道這意味着你需要的VBA代碼添加到每個你運行它的文件?
你可以使它成爲一個加載項,並從外部訪問VBA代碼,這樣你就不需要將代碼複製粘貼到每個文件中。

+0

這正是我所需要的!非常感謝你的幫助。 – NeedToKnowBasis22