score:0
Accepted answer
This sub iterates through all files in a folder and if it finds any XMLs, it calls the second sub
Option Explicit
'in code editor: Tools > References > checkbox in Microsoft Scripting Runtime
Public Sub ProcessXMLs()
Const FOLDER_NAME As String = "C:\Tmp" '<- update this path
Dim tags As Variant, hdrs As Variant, rowID As Long
Dim fso As FileSystemObject, f As File
Set fso = New Scripting.FileSystemObject
hdrs = Array("FileName", "ItemID", "Name", "GivenName", "FamilyName")
tags = Array("FileName", "value", "name", "givenNames", "familyName")
With Sheet1
.Range(.Cells(1, 1), .Cells(1, UBound(tags) + 1)).Value2 = hdrs
rowID = 2
Application.ScreenUpdating = False
For Each f In fso.GetFolder(FOLDER_NAME).Files 'iterate through files
If LCase(fso.GetExtensionName(f)) = "xml" Then
.Cells(rowID, 1).Value2 = fso.GetBaseName(f) & ".xml"
ReadTags Sheet1, fso.OpenTextFile(f.Path, ForReading), rowID, tags
rowID = rowID + 1
End If
Next
.UsedRange.Columns.AutoFit
Application.ScreenUpdating = True
End With
End Sub
This sub extracts values in the 4 tags, one file at the time:
Private Sub ReadTags(ByVal ws As Worksheet, ByVal fsoFile As TextStream, _
ByVal rowID As Long, ByVal tags As Variant)
Dim ln As String, val As String, i As Long, s1 As Long, s2 As Long
With fsoFile
Do While Not .AtEndOfStream 'file stream is open
ln = Trim(.ReadLine) 'read each line
If Len(ln) > 0 Then 'if text line is not empty extract tags
For i = 1 To UBound(tags) 'find each tag - start and closing
s1 = InStr(1, ln, "<" & tags(i), 0)
s2 = InStr(s1 + 1, ln, "</" & tags(i) & ">", 0)
If s1 > 0 And s2 > 0 Then
s1 = InStr(s1, ln, """>", 0) + 2
ws.Cells(rowID, i + 1).Value2 = Trim(Mid(ln, s1, s2 - s1))
Exit For
End If
Next
End If
Loop
End With
End Sub
Result:
Source: stackoverflow.com
Related Query
- how to extract values from multiple XMLs into excel?
- How to extract data from multiple webpages into Excel with a macro
- How to Consolidate Data from Multiple Excel Columns All into One Column
- Access + VBA + SQL - How to export multiple queries into one excel Workbook, but, multiple Worksheet using the criteria from a table
- Excel VBA How to populate a multi-dimensional (3d) array with values from multiple excel ranges?
- How to copy the values from the same column into the next columns? Excel VBA
- How to split a column in Excel into multiple columns, using categories from another column?
- How to loop cell values from excel into a websites text box with VBA?
- Extract Data from a Multiple Web Pages into Excel - using VBA
- How do I extract unique values from multiple columns and use them to populate one column?
- Excel VBA: How to extract date from all cells of a column and put text into another column?
- How to extract unique values from two columns Excel VBA
- How to extract data from HTML divs into Excel
- How to extract values of multiple listboxes on Excel sheet?
- Extract values from mySQL database into Excel using VBA
- Extract multiple lines of data from numerous text files and correlate into Excel
- I am trying to write a script that will extract hourly values from a web page table into excel
- How to find multiple strings from Excel and copy into another spreadsheet using VBA
- Extract certain data from multiple XML files into excel
- How to get particular values from single cell and put into different cells in Excel VBA
- How to get multiple values with a single look up in Excel with transposing the data from column to rows
- How to get multiple values with a single look up in Excel with transposing the data from column to rows
- Excel vba how can extract only a column from a csv into spreadsheet
- combine multiple excel values from an array back into a single excel cell comma separated
- How to extract multiple substrings from an expression using VBA Excel
- How to combine 2D arrays from multiple Excel files into single 2D array?
- How to show multiple values from excel using VBA
- Extract multiple numeric values from string in Python or Excel
- How to extract all drop down values from a data validated cell in excel to single cells for each value?
- How to copy/paste/consolidate values from multiple workbooks into MasterFile with multiple sheets?
More Query from same tag
- Exit Sub if condition not met in function procedure
- change propetries of command button on form navigation in access
- How can I transpose a range with a variable?
- Is there a way to get the opposite value of current boolean without having to implement checks in Visual Basic?
- Excel vba for exporting cell content to TXT file
- Error "Object was open" when trying to edit records in form bound to ADO recordset
- Progress bar using Office VBA
- How to connect to IBM DB2 datasource using VBA in Excel 64bit
- Excel ActiveX ComboBox Displays Previous Value Issue
- Check wether a set of data already exists in current worksheet
- Access, VBA: Find First not working correctly
- Excel VBA macro looping "Save As" increases file size?
- VBA Randomize values from 1 to 30 thereby skipping manually entered values
- How to identify words in Column A Sheet1 that match words in Column B, then copy those Rows to Sheet2?
- Save email to a local folder
- Save data from Macro Enabled Excel to SQL
- VBA Excel loop with offset for extended range of cells
- Microsoft Access 2010 is not saving data in datetime Format
- VBA pass string as range, type mismatch error
- Comparing two cell strings to print out good or bad
- Excel VBA loop with sending request to Api
- Get path of "Program files" folder that contains 32-bit programs
- How to get information if strings in a column are left aligned and how to create new cells with this information in another sheet?
- Can an office addin / app (non-com) add a formula to Excel?
- Processing a Byte Order Mark
- How to Change background Colour of Textbox Using a Button
- Excel Macro: (1) Code below doesn't work (2) How do i make a macro run real time on any sheet
- Arranging collapsed data in a table
- Reference a cell by its column name
- RegEx pattern to remove string between two specific characters