I need the macro to loop if one of the criteria is not met. Working with some basic scraping. I need this to loop until ready before I start gathering data.
If ie.document.getElementsByClassName("clicks").Length < 1 Or ie.document.getElementsByClassName("feedback").Length < 1 Then
Do: Loop
End If
I think it would look like this :
Do While ie.document.getElementsByClassName("clicks").Length >= 1 And ie.document.getElementsByClassName("feedback").Length >= 1
'''code
Loop
Your loop does not check with each iteration, only once and if it is not ready then it will enter an infinite loop. You need a solution that checks each time the loop iterates.
Do
If ie.document.getElementsByClassName("clicks").Length < 1 Or ie.document.getElementsByClassName("feedback").Length < 1 Then
DoEvents 'free the processor to work on other things
Else
Exit Do
End If
Loop
This will check each time the loop iterates rather than just once.
Related
I am trying to create a loop to have Excel tell a DOS based system to search for a condition. If it doesn't find the condition I get stuck in an infinite loop. I have found many situations where a loop will go until it meets a condition. But is there a way to have it run until the condition is met OR an integer reaches a point? I am VERY new to VBA, so take my lack of knowledge lightly please.
I have tried several of the single condition guides, and purchased VBA for dummies, (not a great deal of help)
Sub Test ()
DOS.readscreen StrLoop 3, 1, 4
Do Until StrLoop = "TXT"
Loop
End Sub
I'm hoping to have an integer count to a certain point and if it reaches a point have it exit the loop. I am just uncertain of how to do it.
Dim StrLoop as string, i as long
Do
DOS.readscreen StrLoop 3, 1, 4
i = i + 1
Loop Until StrLoop = "TXT" or i = 1000
You need to change the variable strloop , until get to a value
dim i as long
Do Until StrLoop = "TXT" or i = 1000
DOS.readscreen StrLoop 3, 1, 4
i = i + 1
Loop
The program requires input of positive numbers, and counts each even number using a loop function that doesn't count odds and ends if a O is input.
I'm not sure how to go about creating a loop or if I can use an if function within the loop.
Dim Count = 0
While (number mod 2 = 0) do
Count + 1 = Count
I actually didn't understand the question very well but as far as concerned, if you dont want odd numbers to be included I suggest on count add 2 not one , since the count variable starts with zero do:
Dim Count+2
Btw when do you want the count to stop? At 2 And goes back to 0?
If so then use the if statement
var Dim_count = 0;
if(Dim_count == 0){Dim_count+2}
else if(Dim_count ==2){Dim_Count =0;}
It would help if you provide a sample input so we can work with actual code and guide you to the right solution.
If you receive the input as, let's say, array of numbers, you can simply loop trough it using for or foreach and add additional condition to check for 0 if you want to preliminary exit:
For Each number As Integer In numbers
If (number mod 2 = 0) Then
Count = Count + 1
End If
If (number = 0) Then
Exit For
End If
Next
If you have existing code in which somehow number is reinitialized/redefined on each iteration already, then what you have is pretty close to what you need:
While (number <> 0)
If (number mod 2 = 0) Then
Count = Count + 1
End If
End While
Function counts even numbers:
REM function gets input, exits at 0 and adds positive even numbers.
DO
INPUT X
IF X = 0 THEN PRINT Y; " even numbers": END
IF X > 0 THEN
IF X / 2 = X \ 2 THEN Y = Y + 1
END IF
LOOP
I am completely unsure about this. just a guess...
Dim j as Integer
Dim i As integer
j = 0
i = 2
For i = 1 to 100
j = j+i
Print j
Loop
End Sub
Assuming you are getting numbers from some input, this is how you can do it. Have an infinite loop with While True, then for every number given from your input, check if its even using number mod 2 = 0. This will go on forever so you need to add some condition (another if statement) for it to stop the while loop. More information about while loops here: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/while-end-while-statement
Dim Count = 0
While True do
If (number mod 2 = 0) Then
Count + 1 = Count
End If
End While
I have three different CASEs in a bit of code, each of which has minor variations on a routine revolving around a For...Next loop. The question is, is there any difference in efficiency and speed depending on how I nest them?
In other words, is:
Select Case sPosition
Case Is = "First"
For j = 17 to 65
[Do stuff]
Next j
Case Is = "Middle"
For j = 17 to 65
[Do stuff]
Next j
Case Is = "Last"
For j = 17 to 65
[Do stuff]
Next j
End Select
...any more or less efficient than:
For j = 17 to 65
Select Case sPosition
Case Is = "First"
[Do stuff]
Case Is = "Middle"
[Do stuff]
Case Is = "Last"
[Do stuff]
End Select
Next j
More of a question for CodeReview than SO, but regardless, it is dependent on what you are intending to do with the loops. In the first situation, you have a condition and then you loop through the data in accordance with the result of the condition, doing the same thing for all the data. In the second case, you are re-checking the condition each time the loop runs. If you think different things will be happening as the loop runs (different Cases being selected), then you need to use the second variation, but if the checked condition is not changing, then the first option will be faster, as the condition is only checked once for the loop
I think first variant is more efficient because select case works only one time and in the second variant select case works for every step of cicle (49 times)
I have a part of a macro that is being completely skipped when it is executing. I think I probably has something to do with the if statement combined with the loop.
For x = 1 To HomeLoop
If Application.WorksheetFunction.CountIf(Sheet6.Range("G:G"), Sheets(1).Cells(x + 2, 1)) = 0 Then
Sheets(1).Select
Rows(x + 2).Select
Selection.Delete Shift:=xlUp
Else
End If
Next x
There is more code before and after the block. When I'm going through the code with the debugger, the "For x = 1 to HomeLoop" will highlight and the next step goes directly to highlight the code below the "Next x" I can't figure out why the block is being completely skipped.
Any help is greatly appreciated.
Thank you.
The for loop basically runs an if statement each time it runs through
for x = 1 to homeloop is basically equivalent to if x<=homeloop
my guess is homeloop doesn't have a value in it or is less then 1. Do you have any sort of error handling in the code?
The following code is exhibiting the following bizarre behavior:
1.) if I set the step to zero it moves from cell to cell just fine, the message box counts out 1 through 8 (For i = 1 to 8 Step 0).
2.) if I set the step to one it gives the sequence 1, 3, 5, 7 (For i = 1 to 8 Step 1). It is my understanding that Step 1 should be producing 1, 2, 3, 4, 5, 6, 7, 8 for the message box return.
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To 8 Step 1
MsgBox i
ActiveCell.Offset(1, 0).Range("A1").Select
i = i + 1
Next i
End Sub
The math of this makes sense of course, but the mechanics according to standard excel looping seems bizarre because this yields the same result as (with no step increment specified):
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To 8
MsgBox i
ActiveCell.Offset(1, 0).Range("A1").Select
i = i + 1
Next i
End Sub
that is the MsgBox sequence is 1, 3, 5, 7. The point is that I know appending "Step 0" to the for statement gives the unitary increments but this feels like a work-around for leaving off the Step increment all together? I have to wonder if my 2013 excel pro plus is corrupted. Please clue me in as to whether this is normal or not. TIA.
But you have this line in the code:
i = i + 1
Which makes it jump double because it's your loop variable too!
So For increments the variable for you, you don't have to do i = i + 1 in the loop. You would have to, if it was a while loop. But never in a For loop, because you're interfering with the loop. (Unless this is your exact intention.)
Folks what can I say? Well a little more actually, if you look at the block of code that I didn't include:
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To 8 Step 0
MsgBox i
ActiveCell.Offset(1, 0).Range("A1").Select
i = i + 1
Next i
End Sub
you might be able to appreciate the value of the "Step 0", this allows the user total control over the incremental step via i = i +1, or more generally for a function f(), i = f(i). Hence not incrementing at the "For i = ..." level may actually have some utility depending on what kind of jam one find's oneself in.
In summary: a.) leaving out the Step modifier is equivalent to the default i.e., Step = 1. b.) zeroing the Step modifier, Step = 0 requires a "do while" approach with explicit incrementing via i = f(i) placed within the loop. Finally, and I'm not going to test this, but with Step = 0 and no increasing incrementing may cause an infinite loop or throw an error depending on the situation (so it is probably a good idea to avoid Step = 0 unless you are sure about your f(i) and it's placement within the loop block!).