如何使用VBA将保存在文件夹中的多个.xml文件的文件格式更改为.xlsx文件格式

时间:2016-07-21 19:14:32

标签: xml excel vba excel-vba

我正在尝试将文件夹中的多个.xml文件转换为.xlsx文件。我有超过100个文件,将每个文件手动保存为.xlsx会很繁琐。所以我想到了使用宏。此处的代码仅针对特定文件运行,如何使其适用于我的文件夹中的所有文件。

Sub macroconvt()
'
' macroconvt Macro
'

'
    ChDir _
        "S:\Research and Analysis\Interns\2016\Summer\New RCA Macro\Layout files\converted excel files"
    ActiveWorkbook.SaveAs Filename:= _
        "S:\Research and Analysis\Interns\2016\Summer\New RCA Macro\Layout files\converted excel files\FIRM_Limit Excessive Hours_19JUL16.xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub

谢谢

1 个答案:

答案 0 :(得分:0)

使用FileSystemObject,如下所示:

Dim FSO, tFolder, tFiles, tFile As Object
Dim fp As String
Dim wb as workbook

Set FSO = CreateObject("Scripting.FileSystemObject") ' create FSO via late binding

fp = "Put your folder path here"

Set tFolder = FSO.GetFolder(fp) ' set FSO folder
Set tFiles = tFolder.Files ' get files collection

For Each tFile In tFiles

        Set wb= Application.Workbooks.Open(tFile.Path) ' open the file
        ' do what you want with the workbook e.g. wb.SaveAs etc

Next tFile