2

我有一个小脚本,允许我遍历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

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

知道我的语法哪里出错了吗?另外,是否可以在静音模式下做同样的事情?所以不打开工作簿?

谢谢 !

4

1 回答 1

1

您的问题是您Dir在初始Dir循环中使用第二个来测试和创建xml子目录。

您可以 - 并且应该将其移到循环之外 - 特别是因为它是一次性测试并且不应该从一开始就循环。下面是这样的

(否则你使用Dir得很好,按照我在使用 VBA 循环通过文件夹中的文件中的简单通配符代码示例?

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
于 2013-07-06T04:53:12.723 回答