While loop question

rick051112

New Member
Joined
Aug 14, 2023
Messages
3
Office Version
  1. 2016
Here is my code:

Sub number()
Dim x as Integer
Do While Cells(x,3) <> 4 Or Cells(x,3).Cells.Offset(0,1) <> 4

x= x + 1
Loop
End Sub

My data is as follow:
C2= 3 D2= 4
C3= 4 C3= 3
C4= 4 D4 = 4


This is a simple example, but I am wondering why "Or" works in the while loop instead of using "And"? Basically the Or statement will take the while loop to the last row, but the And statement stops at the first row.

Thanks!!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Welcome to the Board!

Please explain, in plain English, how you want this to work.
What is the logic?
What should this be doing?

Also, for this part:
VBA Code:
Cells(x,3).Cells.Offset(0,1)
why not just use this?
VBA Code:
Cells(x,4)
 
Upvote 0
In the context of just addressing the why of it, here's your first iteration with the first row of values:
Do While 3 <> 4 AND 4 <> 4 is obviously not True so the loop is over.
Do While 3 <> 4 OR 4 <> 4: one of them is True so the loop continues. OR means Do While this OR that. As long as one of the This or That is True, it will loop.

Logical operators can sometimes be a bit confusing, especially when 2 or more are used in the same evaluation. They often need to be grouped so that the AND and the OR are evaluated in the correct order for the logical tests.
 
Upvote 0
Welcome to the Board!

Please explain, in plain English, how you want this to work.
What is the logic?
What should this be doing?

Also, for this part:
VBA Code:
Cells(x,3).Cells.Offset(0,1)
why not just use this?
VBA Code:
Cells(x,4)

Ty for your reply! I am new to vba.

Basically it should continue to loop through the row if the conditions are not met in columns C and D.

So if cell C2= 3 and cell D2 = 4, it will keep on looping until it finds a row where the value in column C = 4 and the value in column D = 4.

My question is that it seems to work with the "Or" statement in the while loop and not the "And" statement. But this seems to be backwards. Why is that?
 
Upvote 0
My question is that it seems to work with the "Or" statement in the while loop and not the "And" statement. But this seems to be backwards. Why is that?
Its not, if you break it down and think about.

Basically, you want it to stop when BOTH columns C and D are 4, right?
So, you basically you want it to keep running if at least one of the values in C or D is NOT 4. That is an OR statement (since you are checking for NOT equal to 4).

If you use AND, you tell it to keep running only when BOTH C and D are NOT 4 (not either one).
So if either one is 4, it will stop.
Since column D is 4 in your first example, that is where it would stop.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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