作者:森眺
链接:https://www.zhihu.com/question/39234324/answer/284625834
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Sub CAVToXLSX()
Dim fDir As String
Dim wB As Workbook
Dim wS As Worksheet
Dim fPath As String
Dim sPath As String
fPath = "C:\Users\Micro\Desktop\source\"
sPath = "C:\Users\Micro\Desktop\target\"
fDir = Dir(fPath)
Do While (fDir <> "")
If Right(fDir, 4) = ".csv" Or Right(fDir, 5) = ".csv" Then
On Error Resume Next
Set wB = Workbooks.Open(fPath & fDir)
'MsgBox (wB.Name)
For Each wS In wB.Sheets
wS.SaveAs sPath & wB.Name & ".xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Next wS
wB.Close False
Set wB = Nothing
End If
fDir = Dir
On Error GoTo 0
Loop
End Sub
这段代码是一个VBA宏,用于批量将指定文件夹中所有.csv或.CSV文件转换为.xlsx格式。它首先设置源文件夹和目标文件夹路径,然后遍历源文件夹,找到所有.csv文件,逐一打开,保存为.xlsx格式,并存放在目标文件夹下。
1458

被折叠的 条评论
为什么被折叠?



