logic if end if..

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

logic if end if..

Post by sal21 »

If Mid$(TEST, 1, 2) = "OP" And EROGATI > 0 Or INTROITATI And Not EROGA = "OK" And Not INTROI = "OK" Then

end if

i need to exclude EROGATI and INTROITATI if the condition EROGA and INTROI is "OK" and "viceversa" is correct?

User avatar
HansV
Administrator
Posts: 78489
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: logic if end if..

Post by HansV »

I don't really understand what you're asking, so this is a general reply.

And has precedence above Or, so your condition can be read as

If (Mid$(TEST, 1, 2) = "OP" And EROGATI > 0) Or (INTROITATI And Not EROGA = "OK" And Not INTROI = "OK") Then

If you want something different, you can use parentheses ( ) to force a different interpretation, for example

If Mid$(TEST, 1, 2) = "OP" And (EROGATI > 0 Or INTROITATI) And Not EROGA = "OK" And Not INTROI = "OK" Then
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: logic if end if..

Post by sal21 »

HansV wrote:I don't really understand what you're asking, so this is a general reply.

And has precedence above Or, so your condition can be read as

If (Mid$(TEST, 1, 2) = "OP" And EROGATI > 0) Or (INTROITATI And Not EROGA = "OK" And Not INTROI = "OK") Then

If you want something different, you can use parentheses ( ) to force a different interpretation, for example

If Mid$(TEST, 1, 2) = "OP" And (EROGATI > 0 Or INTROITATI) And Not = "OK" And Not INTROI = "OK" Then

in effect i need to use only one pair of condition or the first pair ( EROGATI and INTROITATI)or the second pair ( EROGA and INTROI) when Mid$(TEST, 1, 2) = "OP" is true...

User avatar
HansV
Administrator
Posts: 78489
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: logic if end if..

Post by HansV »

Please explain very precisely what the conditions for each should be.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: logic if end if..

Post by agibsonsw »

I don't follow either, but I have a couple of suggestions that might help:

Put the most significant test first - this helps to break down the statement;
Introduce boolean variables - this will make it easier to test, and easier to create your test condition(s):

Code: Select all

    Dim blnOk As Boolean
    'EROGA = "OK"
    'INTROI = "OK"
    blnOk = EROGA = "OK" Or INTROI = "OK"
    Debug.Print blnOk
Use brackets (Hans :cheers: );
Try to write the conditions in a way that reduces the use of 'Not'. 'Not' is harder to follow/ keep track of when reading the statement.

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: logic if end if..

Post by macropod »

Hi Sal,

Something the others haven't mentioned is that having all those tests in a single statement is very inefficient, as VBA has to test all of them before being able to move on. A more efficient construct (without commenting on whether it does what you want), would be something like:

Code: Select all

If Mid$(TEST, 1, 2) = "OP" Then
  If EROGATI > 0 Or INTROITATI Then
    If EROGA <> "OK" And INTROI <> "OK" Then
      'Do something
    End If
  End If
End If
Paul Edstein
[Fmr MS MVP - Word]