Problem with the while loop in VBA

Solved
buffalo31 Posted messages 29 Status Member -  
buffalo31 Posted messages 29 Status Member -
Hello,

I'm currently working on a small code in VBA Excel, but I'm having a problem with the While loop that doesn't incorporate the two conditions I associated with it:

ac = Worksheets("Interface").Cells(3, 3).Value
eng = Worksheets("Interface").Cells(4, 3).Value

While Worksheets("DataSheet").Cells(i, 1).Value <> ac And Worksheets("DataSheet").Cells(i, 2).Value <> eng
i = i + 1
Wend
......
.......
When I execute the code, as soon as the first condition is met, it exits the loop without considering the second condition.

So how can I make my While loop take into account both conditions? I tried enclosing the two conditions in parentheses, but it doesn't change anything.

Thank you in advance
Configuration: Windows XP Internet Explorer 6.0

3 answers

  1. le père
     
    Hello

    the VBA while handles an "and" between two conditions perfectly, I think it's rather you who has a little problem expressing what you want ;)

    Can you give some examples (with values) of cases where the loop should continue and others where it should stop? You will probably realize your mistake yourself.
    1
    1. Anonymous user
       


      While ((Worksheets("DataSheet").Cells(i, 1).Value <> ac) And ((Worksheets("DataSheet").Cells(i, 2).Value <> eng ))

      mais sinon il a raison pour les opérateurs logiques en VBA voici le cours

      https://www.commentcamarche.net/contents/1188-vbscript-les-operateurs

      --
      Les gens qui disent que windows est pourri et qui continuent de l'utiliser me font bien rire ...
      95 % des erreurs proviennent de ce qu'il y a entre le clavier et la chaise
      0
    2. buffalo31 Posted messages 29 Status Member
       
      I tried using the parentheses as you indicated, but it still doesn't change. Here's an example:

      In Worksheets("DataSheet").Cells(i, 1) I have airplanes, and in Worksheets("DataSheet").Cells(i, 2) I have engines.

      I enter into a sheet called Worksheets("Interface") an airplane (ac) and an engine (eng) respectively "B747" and "PW4000"

      The idea is to retrieve the row i from Worksheets("DataSheet") where Worksheets("DataSheet").Cells(i, 1) = "B747" and Worksheets("DataSheet").Cells(i, 2) = "PW4000" for further processing.

      When I enter the engine that corresponds to the right airplane, there’s no problem

      But when I enter an engine that does not correspond to the airplane, I expect it to find nothing; at best, I put a condition to exit the loop. But to my great surprise, it retrieves row i from the first condition that is met.

      So basically, the two conditions seem dissociated, which I don't understand.
      0
  2. le père
     
    you want to find Worksheets("DataSheet").Cells(i, 1) = "B747" and Worksheets("DataSheet").Cells(i, 2) = "PW4000"
    So you need to loop while Worksheets("DataSheet").Cells(i, 1) <> "B747" OR Worksheets("DataSheet").Cells(i, 2) <> "PW4000"
    0
  3. buffalo31 Posted messages 29 Status Member
     
    Great, that works! I was messing up the negation in my logical operators, lol! A little refresher on logic in math wouldn't hurt me.
    Thanks again, and see you soon!
    0