Excel 将工作簿另存为*.xml到子文件夹时的目录问题
我有一个小脚本,可以遍历当前文件夹中的所有Excel 将工作簿另存为*.xml到子文件夹时的目录问题,excel,dir,vba,Excel,Dir,Vba,我有一个小脚本,可以遍历当前文件夹中的所有xslx文件,并将它们全部保存为xml工作表 这很好,但我想将它们保存在子文件夹中,这就是问题所在,因为我总是再次保存同一个文件。我不太熟悉Dir语法,所以如果有人能帮我一点忙,我将非常感激 该部分按预期工作: Sub XLS2XML() Application.DisplayAlerts = False Dim folderPath As String Dim Report As String Dim ReportName As String Dim
xslx
文件,并将它们全部保存为xml
工作表
这很好,但我想将它们保存在子文件夹中,这就是问题所在,因为我总是再次保存同一个文件。我不太熟悉Dir
语法,所以如果有人能帮我一点忙,我将非常感激
该部分按预期工作:
Sub XLS2XML()
Application.DisplayAlerts = False
Dim folderPath As String
Dim Report As String
Dim ReportName As String
Dim XMLLocation As String
Dim XMLReport As String
Dim WB As Workbook
'set path to current location
folderPath = ThisWorkbook.Path
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
'loop through all xlsx files
Report = Dir(folderPath & "*.xlsx")
Do While Report <> ""
Set WB = Workbooks.Open(folderPath & Report)
'get the file name without path
ReportName = Split(Report, ".")(0)
XMLLocation = folderPath
XMLReport = XMLLocation & ReportName & ".xml"
'save the file as xml workbook
ActiveWorkbook.SaveAs filename:=XMLReport, _
FileFormat:=xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False
'close and next
WB.Close False
Report = Dir
Loop
MsgBox "All XML files have been created"
Application.DisplayAlerts = True
End Sub
Sub-XLS2XML()
Application.DisplayAlerts=False
将folderPath设置为字符串
将报表设置为字符串
将ReportName设置为字符串
Dim XMLLocation作为字符串
Dim XMLReport作为字符串
将WB设置为工作簿
'将路径设置为当前位置
folderPath=ThisWorkbook.Path
如果正确(folderPath,1)“\”则folderPath=folderPath+“\”
'循环浏览所有xlsx文件
Report=Dir(folderPath&“*.xlsx”)
报告时执行“”
设置WB=工作簿。打开(文件夹路径和报告)
'获取不带路径的文件名
报表名称=拆分(报表“.”(0)
XMLLocation=folderPath
XMLReport=XMLLocation&ReportName&“.xml”
'将文件另存为xml工作簿
ActiveWorkbook.SaveAs文件名:=XMLReport_
FileFormat:=xlXMLSpreadsheet,ReadOnlyRecommended:=False,CreateBackup:=False
"关,下一个"
WB.关闭错误
报告=目录
环
MsgBox“已创建所有XML文件”
Application.DisplayAlerts=True
端接头
这一条对我来说是失败的:
Sub XLS2XML()
Application.DisplayAlerts = False
Dim folderPath As String
Dim Report As String
Dim ReportName As String
Dim XMLLocation As String
Dim XMLReport As String
Dim WB As Workbook
'set path to current location
folderPath = ThisWorkbook.Path
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
'loop through all xlsx files
Report = Dir(folderPath & "*.xlsx")
Do While Report <> ""
Set WB = Workbooks.Open(folderPath & Report)
'get the file name without path and save it in xml folder
ReportName = Split(Report, ".")(0)
XMLLocation = folderPath & "xml"
XMLReport = XMLLocation & "\" & ReportName & ".xml"
'create xml folder if it doesn't exist yet
If Len(Dir(XMLLocation, vbDirectory)) = 0 Then
MkDir XMLLocation
End If
'save the file as xml workbook
ActiveWorkbook.SaveAs filename:=XMLReport, _
FileFormat:=xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False
'close and next
WB.Close False
Report = Dir
Loop
Sub-XLS2XML()
Application.DisplayAlerts=False
将folderPath设置为字符串
将报表设置为字符串
将ReportName设置为字符串
Dim XMLLocation作为字符串
Dim XMLReport作为字符串
将WB设置为工作簿
'将路径设置为当前位置
folderPath=ThisWorkbook.Path
如果正确(folderPath,1)“\”则folderPath=folderPath+“\”
'循环浏览所有xlsx文件
Report=Dir(folderPath&“*.xlsx”)
报告时执行“”
设置WB=工作簿。打开(文件夹路径和报告)
'获取不带路径的文件名并将其保存在xml文件夹中
报表名称=拆分(报表“.”(0)
XMLLocation=folderPath&“xml”
XMLReport=XMLLocation&“\”&ReportName&“.xml”
'如果xml文件夹尚不存在,则创建该文件夹
如果Len(Dir(XMLLocation,vbDirectory))=0,则
MkDir xml位置
如果结束
'将文件另存为xml工作簿
ActiveWorkbook.SaveAs文件名:=XMLReport_
FileFormat:=xlXMLSpreadsheet,ReadOnlyRecommended:=False,CreateBackup:=False
"关,下一个"
WB.关闭错误
报告=目录
环
知道我的语法哪里出错了吗?另外,是否可以在静默模式下执行相同的操作?那么不打开工作簿
谢谢 您的问题是在初始
Dir
循环中使用第二个Dir
来测试和创建xml子目录
您可以-并且应该将其移到循环之外-尤其是因为它是一次性测试,不应该一开始就循环。下面是这样的
(根据中我的简单通配符代码示例,您使用了Dir
fine)
Sub-XLS2XML()
Application.DisplayAlerts=False
将folderPath设置为字符串
将报表设置为字符串
将ReportName设置为字符串
Dim XMLlocation作为字符串
Dim XMLReport作为字符串
将WB设置为工作簿
'将路径设置为当前位置
folderPath=ThisWorkbook.Path
XMLlocation=folderPath&“xml”
如果Len(Dir(XMLlocation,vbDirectory))=0,则MkDir XMLlocation
如果右$(folderPath,1)“\”则folderPath=folderPath+“\”
'循环浏览所有xlsx文件
Report=Dir(folderPath&“*.xlsx”)
当Len(报告)>0时执行
设置WB=工作簿。打开(文件夹路径和报告)
'获取不带路径的文件名并将其保存在xml文件夹中
报表名称=拆分(报表“.”(0)
XMLReport=XMLlocation&“\”&ReportName&“.xml”
'将文件另存为xml工作簿
WB.SaveAs文件名:=XMLReport_
FileFormat:=xlXMLSpreadsheet,ReadOnlyRecommended:=False,CreateBackup:=False
"关,下一个"
WB.关闭错误
报告=目录
环
端接头
Sub XLS2XML()
Application.DisplayAlerts = False
Dim folderPath As String
Dim Report As String
Dim ReportName As String
Dim XMLlocation As String
Dim XMLReport As String
Dim WB As Workbook
'set path to current location
folderPath = ThisWorkbook.Path
XMLlocation = folderPath & "xml"
If Len(Dir(XMLlocation, vbDirectory)) = 0 Then MkDir XMLlocation
If Right$(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
'loop through all xlsx files
Report = Dir(folderPath & "*.xlsx")
Do While Len(Report) > 0
Set WB = Workbooks.Open(folderPath & Report)
'get the file name without path and save it in xml folder
ReportName = Split(Report, ".")(0)
XMLReport = XMLlocation & "\" & ReportName & ".xml"
'save the file as xml workbook
WB.SaveAs Filename:=XMLReport, _
FileFormat:=xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False
'close and next
WB.Close False
Report = Dir
Loop
End Sub