VBA将文件夹中的csv文件转换为xlsx文件

时间:2015-06-10 13:31:51

标签: excel vba excel-vba

我有这段代码。

此代码转换为xlsm文件。

我想转换为xlsx文件。

如何?

我尝试改变

wBook.SaveAs XlsFolder & Replace(fname, ".csv", ""), ThisWorkbook.FileFormatTO
wBook.SaveAs XlsFolder & Replace(fname, ".csv", ".xlsx")

没用。

Private Sub CommandButton2_Click()
Dim CSVfolder As String
 Dim XlsFolder As String
 Dim fname As String
 Dim wBook As Workbook

 CSVfolder = "C:\csv\"
 XlsFolder = "C:\Charts\"

 fname = Dir(CSVfolder & "*.csv")

 Do While fname <> ""
    Set wBook = Workbooks.Open(CSVfolder & fname, Format:=6, Delimiter:=",")
    wBook.SaveAs XlsFolder & Replace(fname, ".csv", ""), ThisWorkbook.FileFormat
    wBook.Close False
 fname = Dir
 Loop
End Sub

1 个答案:

答案 0 :(得分:2)

使用宏重新编码器,xlsx工作簿的文件格式为FileFormat:=xlOpenXMLWorkbook

所以这是你的代码:

Private Sub CommandButton2_Click()
Dim CSVfolder As String, _
    XlsFolder As String, _
    fname As String, _
    wBook As Workbook

 CSVfolder = "C:\csv\"
 XlsFolder = "C:\Charts\"

 fname = Dir(CSVfolder & "*.csv")

 Do While fname <> ""
    Set wBook = Workbooks.Open(CSVfolder & fname, Format:=6, Delimiter:=",")
    wBook.SaveAs XlsFolder & Replace(fname, ".csv", ""), xlOpenXMLWorkbook
    wBook.Close False
    fname = Dir
 Loop
End Sub