VBA Excel - How to pull XML elements

Viewed 102

I'm trying to pull an element using the previous element from an XML file using Excel VBA. The XML file repeats the nodes for each element and are not unique. I've tried searching for the first element's text (Test 1) and then using .Offset to pull the next element (1111) however, this is not working. Help?

XML Data Sample

<Content>
<Dropdown>Test 1</Dropdown>
<amount>1111</amount></Content>
<Content>
<Dropdown>Test 2</Dropdown>
<amount>2222</amount></Content>

Sample of my VBA Code:

 Dim check_cell          As Range
 Dim ws_input            As Worksheet
 Dim ws_Test             As Worksheet

Set ws_input = Application.Workbooks.Open(Test_file).Sheets(1)

For Each check_cell In ws_input.Range(ws_input.Cells(2, 1), ws_input.Cells(2, Last_Column(ws_input, 2)))
    
        Select Case True               
          Case check_cell.Text Like "/Content/Dropdown*"
          If check_cell.Offset(1, 0) = "Test 1" Then
           LocateField("Test 1?", ws_Test).Offset(0, 1) = check_cell.Offset(1, 1)
           End If
           End Select
    Next check_cell
End Sub

Public Function LocateField(desc As String, ws As Worksheet, Optional after_range As Variant) As Range

    On Error Resume Next
    Set LocateField = ws.Cells.Find(what:=desc, After:=after_range, LookIn:=xlValues, SearchOrder:=xlByRows, MatchByte:=False)
    On Error GoTo 0
    
    
    If LocateField Is Nothing Then
        MsgBox "Field not found."
    End If

    
End Function
0 Answers
Related