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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30757897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:40:22  来源:igfitidea点击:

VBA converting csv Files in a folder to xlsx Files

excelvbaexcel-vba

提问by Abdul Shiyas

I have this code.

我有这个代码。

This code converts to xlsm files.

此代码转换为 xlsm 文件。

I want to convert to xlsx files.

我想转换为 xlsx 文件。

How?

如何?

I tried by changing

我尝试通过改变

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

It didn't worked.

它没有用。

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

回答by R3uK

Using the macro recoder, the file format for an xlsxworkbook is FileFormat:=xlOpenXMLWorkbook

使用宏记录器,xlsx工作簿的文件格式为FileFormat:=xlOpenXMLWorkbook

So here is your code :

所以这是你的代码:

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