Home > Net >  How to write a vba loop like a c for loop
How to write a vba loop like a c for loop

Time:03-17

I am wanting to write a loop similar to this:

for (int counter = 2; err.number != 0; counter    {
    bot.FindElementsByCss(".slicerCheckbox")([counter]).Click;
    }

how would I do that in vba: I currently have something like:

Dim counter As Integer
counter = 2
retry:
On Error Resume Next
bot.FindElementsByCss(".slicerCheckbox")([counter]).Click
if err.Number <> 0 then
   On Error GoTo -1
   On Error GoTo 0
   counter = counter   1
   goto retry:
end if 
on error goto -1
on error goto 0

CodePudding user response:

You could use a do loop

Dim counter as Integer
counter=2

Do 

    on error resume next
    bot.FindElementsByCss(".slicerCheckbox")([counter]).Click
    if err.number = 0 then exit do
    err.clear
    counter=counter 1

Loop


CodePudding user response:

Here, you are using selenium to click on several web elements. It seems as though you are referencing them by number, counting up as you go. You know that you have reached the end when you have an error referencing the web element by number.

I think you would be better off just capturing the whole set of elements, then clicking on each one. That way, you don't have to respond to an error condition.

Sub process_elements()

    Dim elems As WebElements
    Dim elems As WebElement
  
    Set elems = bot.FindElementsByCss(".slicerCheckbox")
  
    For Each elem In elems
        elem.Click
    Next

End Sub
  • Related