VBA: Loop enter data into form based on cell values

noctash

New Member
Joined
Aug 4, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hello! I have an array of data, Column C is the ElementIDs of each box on a web form, and Column D is what I want to set the ElementID to.

I know how to do this for a single ElementID as follows:

VBA Code:
Dim IE As New SHDocVw.InternetExplorerMedium
Set IE = New SHDocVw.InternetExplorerMedium
Dim htmldoc As MSHTML.HTMLDocument
Set htmldoc = IE.Document
Dim element As MSHTML.IHTMLElement
Set element = htmldoc.getElementById("elementid")
element.Value = "value"

But I have a case where each element ID is a row in a column, and each value is the corresponding cell to the right.
I have tried the following but am getting an error that "Element Value" is not defined.

VBA Code:
Sub DataEntry()
'
'
'
Range("C2").Select
Range("C2").Formula2 = "formula that determines the name of the element ID that needs to be filled since they're all in order"
Range("D2").Select
Range("D2").Formula2 = "formula thats matches data from another sheet to go in each form box"

'loop through cells
Dim maxIDs As Long
maxIDs = Cells(Rows.Count, 1).End(xlUp).Row

Dim ElementIDArray As Range
Dim ElementValueArray As MSHTML.IHTMLElement

For Each ElementIDArray In Range("C1:C" & maxIDs).Cells
    Set ElementValueArray = htmldoc.getElementById(ElementIDArray)
    ElementValueArray.Value = ElementIDArray.Offset(0, 1)
Next ElementIDArray

End Sub
Any help or advice is appreciated. Thank you!
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I figured it out. I feel very silly. ;)

VBA Code:
Dim IDArray As Range
Dim ValueArray As Range

Dim IDValue As MSHTML.IHTMLElement

Dim x As Integer
For x = 2 To maxIDs

Set IDArray = Range("C" & x)
Set ValueArray = Range("D" & x)

Set IDValue = htmldoc.getElementById(IDArray.Value)
IDValue.Value = ValueArray.Value
 
Upvote 0
Solution

Forum statistics

Threads
1,214,982
Messages
6,122,573
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top