我正在使用 InfoPath 表单从现场团队收集数据,所有已完成的表单都保存为标准 InfoPath XML 格式。现在我想将所有这些 XML 文件导入 Excel 以编译报告。我一次只做一个 XML 文件,但这当然很愚蠢。无论如何,一位朋友编写了这个 Excel VB 宏代码,用于将多个 XML 文件导入 Excel 工作表选项卡(XMLData),然后将数据整齐地复制到另一个选项卡(结果)。问题是 XML 文件中的数据被乱序导入 Excel 列。对此代码是否有简单的修复?这是 Excel VB 宏代码:
Sub ReadXML()
    Dim strFile As String
    MsgBox "I'll start reading please don't touch the computer"
    Dim strPath As String
    Dim colFiles As New Collection
    Dim i As Integer
    Dim wb As Workbook
    strPath = ActiveSheet.Range("C2")
    strFile = Dir(strPath)
    While strFile <> ""
        colFiles.Add strFile
        strFile = Dir
    Wend
    If colFiles.Count > 0 Then
        For i = 1 To colFiles.Count
             Application.ScreenUpdating = False
             Application.DisplayAlerts = False
             strTargetFile = strPath & colFiles(i)
             Set wb = Workbooks.OpenXML(Filename:=strTargetFile, LoadOption:=xlXmlLoadOpenXml)
             Application.DisplayAlerts = True
             wb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("XMLDATA").Range("A" & i * 3 + 1)
             ThisWorkbook.Sheets("XMLDATA").Rows(i * 3 + 3).Copy ThisWorkbook.Sheets("Result").Range("A" & i + 2)
             wb.Close False
             Application.ScreenUpdating = True
             ActiveSheet.Range("P2") = i
        Next i
        ThisWorkbook.Sheets("XMLDATA").Rows(4).Copy ThisWorkbook.Sheets("Result").Rows(1)
        ThisWorkbook.Sheets("XMLDATA").Rows(5).Copy ThisWorkbook.Sheets("Result").Rows(2)
        MsgBox "I'm Done!"
    End If
End Sub
也许有一个更简单更优雅的解决方案将 InfoPath XML 文件导入 Excel,只是还没有找到。非常感谢您的帮助。