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

我有这个代码.

此代码转换为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
最佳答案
使用宏重新编码器,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

转载注明原文:excel – VBA将文件夹中的csv文件转换为xlsx文件 - 代码日志