'Addressing specific item within HTML string using VBA

I am trying to use VBA to populate a webform and I am struggling to correctly address the field I want to populate.

This is one line of the table in HTML

enter image description here

Here is the overall HTML structure enter image description here

<th>
    <label for="location_sales_target_2_2022_15_target_val">2022w15 (03/04)</label>
</th>

<td>
    <input name="location_sales_target[2][2022/15][id]" id="location_sales_target_2_2022_15_id" type="hidden" value="12751">
    <input name="location_sales_target[2][2022/15][target_val]" id="location_sales_target_2_2022_15_target_val" type="text" value="0.00">
</td>
<td>£2,097.33</td>

I need to be able to address the final value field and update its value.

This is the VBA code I have so far. My VBA is limited and this is copied from online and modified.

Sub IEWebScrape1()
    Dim IE As InternetExplorer 'Reference to Microsoft Internet Controls
    Set IE = New InternetExplorer
 
    With IE
        .Visible = True
        .Navigate2 "https://"
        
        'we add a loop to be sure the website is loaded and ready.
        'Does not work consistently. Cannot be relied upon.
        Do While .Busy = True Or .readyState <> READYSTATE_COMPLETE 'Equivalent = .ReadyState <> 4
            ' DoEvents - worth considering. Know implications before you use it.
            Application.Wait (Now + TimeValue("00:00:01")) 'Wait 1 second, then check again.
        Loop
        
             
        'Print info in immediate window
        With .document 'the source code HTML "below" the displayed page.
            Debug.Print.getElementById("sf_admin_container").Children(1).getElementsByTagName("tr")(16).textContent
           
        End With '.document
        
     '   .Quit 'close the application window
   End With 'ie
    
End Sub

The VBA code produces this result, which confirms it is correctly referencing the record I am trying to reference.

2022W15 (03/04)

£2,097.33

How do I correctly address the specific element within the record?



Solution 1:[1]

I have solved the problem now. Obviously completely confused about using getElement statements. Problem solved by correct use of getElementById

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Healey