0

I am trying to make a VBA that can read the HTML and checked if a specific check box is checked and write either check or unchecked in a cell. But I am having difficulties with VBA as I do not use it as often, any advise will be appreciated.

HTML

<input id="foo1" type="checkbox" name="Device" value="iPad" checked="checked"> 

VBA

Sub getValue() Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application") Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") Dim Country As String With IE .Visible = False .navigate ws.Range("A3").Value Do DoEvents Loop Until .readyState = 4 End With Set oShell = CreateObject("WScript.Shell") Dim document document.getElementById("checkBox(iPad)") Item(0).Checked = True End Sub 
1
  • 1
    Are you able to share the website? Commented Aug 1, 2018 at 5:37

2 Answers 2

1

Try

Debug.Print ie.document.querySelector("#foo1").getAttribute("checked") ="checked" 

I am not sure, without an URL to test with whether there is .Checked property you can evaluate for True ( ie.document.querySelector("#foo1").Checked)

Without more HTML hard to say if this will be able to access the required element. There may be forms/iframes/frames to negotiate.

Sign up to request clarification or add additional context in comments.

1 Comment

Any feedback at all please?
0

This can also be done with looping all the input elements on the website until you find the one with the right name, this will of course be slower then a querySelector, but can be usefull if you need to change multiple input elements.

Dim objCollection as Object Set objCollection = ie.Document.getElementsByTagName("input") i = 0 'Loop through all elements and find the checkbox While i < objCollection.Length If objCollection(i).Name = "Device" Then objCollection(i).Checked = False End If i = i + 1 Wend 

If you only have 1 checkbox i would no doubt go with a querySelector as @QHarr

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.