簡體   English   中英

使用VBA在Excel中為單個XML重復行

[英]Rows are repeating for a single XML in Excel using VBA

我正在網上尋找以下宏,以將數據從XML提取到Excel。

Sub ListFiles()
'LISTFILES AND LISTMYFILES MODIFIED FROM

'--------------------------------------------------------------------
'DECLARE AND SET VARIABLEs
Dim ShellApplication As Object
Set ShellApplication = CreateObject("Shell.Application").BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
Path = ShellApplication.self.Path
Set ShellApplication = Nothing
[a3] = "XML"
[b3] = "Files"
'--------------------------------------------------------------------
'DEFAULT PATH FROM HIDDEN SHEET
Call ListMyFiles(Path, True)
End Sub

Sub ListMyFiles(mySourcePath, IncludeSubfolders)
    Set MyObject = New Scripting.FileSystemObject
    Set mySource = MyObject.GetFolder(mySourcePath)
    'With SearchXML
    Application.ScreenUpdating = False

    '--------------------------------------------------------------------
    'FIND XML FILES ONLY, APPLY SEARCH CRIERIA, DISPLAY MATCHES ONLY
    For Each myfile In mySource.Files
        If Right(myfile.Name, 3) = "XML" Or Right(myfile.Name, 3) = "xml" Then 'IS XML?
            LastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row

            '-------------------------------------------------------------
            'IMPORT XML FILE
            ActiveWorkbook.XmlImport URL:=mySource & "\" & myfile.Name, _
                ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A$" & LastRow + 1)
            '------------------------------------------------------------
            'DELETE MAPS
            maps = ActiveWorkbook.XmlMaps.Count
            For I = 1 To maps
                ActiveWorkbook.XmlMaps(1).Delete
            Next I
        End If
    Next
    If IncludeSubfolders Then 'SEARCH SUBFOLDERS FOR SAME CRITERIA
        For Each MySubFolder In mySource.SubFolders
            Call ListMyFiles(MySubFolder.Path, True)
        Next
    End If
    'End With
    Application.ScreenUpdating = True
End Sub

Public Sub ClearSheet()
    Cells.Select
    Selection.ClearContents
    [a1].Select
End Sub

但是,它為某些XML生成多行,但是我希望為單個XML生成一行。

它可能取決於您導入的XML,但是有些部分可能需要更正:

maps = ActiveWorkbook.XmlMaps.Count
For I = 1 To maps
    ActiveWorkbook.XmlMaps(1).Delete
Next I

您應該向后瀏覽,添加Step -1

maps = ActiveWorkbook.XmlMaps.Count
For i = 1 To maps Step -1
    ActiveWorkbook.XmlMaps(i).Delete
Next i

希望能有所幫助! ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM