PC Review


Reply
Thread Tools Rate Thread

Conditional "If Not ... Then" Statement not working properly Help!

 
 
Dan Thompson
Guest
Posts: n/a
 
      25th May 2009
The following code should pop up a message box but it is not doing so where
am I going wrong ?


Sub test()
Dim one As Boolean, two As Boolean, three As Boolean
one = True
two = True
three = False

If Not one = True And Not two = True And Not three = False Then
MsgBox "One or more of the 3 conditions are false"
End If
End Sub

Dan Thompson

 
Reply With Quote
 
 
 
 
Dan Thompson
Guest
Posts: n/a
 
      25th May 2009
There is a correction with the original post a small typo but it still is not
working once I fix the typo

Sub test()
Dim one As Boolean, two As Boolean, three As Boolean
one = True
two = True
three = False

If Not one = True And Not two = True And Not three = True Then
MsgBox "One or more of the 3 conditions are false"
End If
End Sub


"Dan Thompson" wrote:

> The following code should pop up a message box but it is not doing so where
> am I going wrong ?
>
>
> Sub test()
> Dim one As Boolean, two As Boolean, three As Boolean
> one = True
> two = True
> three = False
>
> If Not one = True And Not two = True And Not three = False Then
> MsgBox "One or more of the 3 conditions are false"
> End If
> End Sub
>
> Dan Thompson
>

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      25th May 2009
The criteria part of the statement is false, therefor, VBA will not execute
the command line. Try this statement:

If one = True And two = True And three = False Then
MsgBox "One or more of the 3 conditions are false"
End If



"Dan Thompson" <(E-Mail Removed)> wrote in message
news:B43C158A-196E-46BC-9B27-(E-Mail Removed)...
> The following code should pop up a message box but it is not doing so
> where
> am I going wrong ?
>
>
> Sub test()
> Dim one As Boolean, two As Boolean, three As Boolean
> one = True
> two = True
> three = False
>
> If Not one = True And Not two = True And Not three = False Then
> MsgBox "One or more of the 3 conditions are false"
> End If
> End Sub
>
> Dan Thompson
>



 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      25th May 2009
Also:

If Not one = True Or Not two = True Or Not three = False Then
MsgBox "One or more of the 3 conditions are false"
End If



"Dan Thompson" <(E-Mail Removed)> wrote in message
news:B43C158A-196E-46BC-9B27-(E-Mail Removed)...
> The following code should pop up a message box but it is not doing so
> where
> am I going wrong ?
>
>
> Sub test()
> Dim one As Boolean, two As Boolean, three As Boolean
> one = True
> two = True
> three = False
>
> If Not one = True And Not two = True And Not three = False Then
> MsgBox "One or more of the 3 conditions are false"
> End If
> End Sub
>
> Dan Thompson
>



 
Reply With Quote
 
Dan Thompson
Guest
Posts: n/a
 
      25th May 2009
Thank you for your input. However the code you wrote here will not work
for what I am trying to do because any of the variables "one" "two" or
"three" in the final macro will be true sometimes and other times they can be
false. I want to have a conditional statement that will check to see if all
are true and if either of the 3 variables are false then execute my code.

In your example the condition is depending on the variable "three" to be
false when it may be true sometimes.


"JLGWhiz" wrote:

> The criteria part of the statement is false, therefor, VBA will not execute
> the command line. Try this statement:
>
> If one = True And two = True And three = False Then
> MsgBox "One or more of the 3 conditions are false"
> End If
>
>
>
> "Dan Thompson" <(E-Mail Removed)> wrote in message
> news:B43C158A-196E-46BC-9B27-(E-Mail Removed)...
> > The following code should pop up a message box but it is not doing so
> > where
> > am I going wrong ?
> >
> >
> > Sub test()
> > Dim one As Boolean, two As Boolean, three As Boolean
> > one = True
> > two = True
> > three = False
> >
> > If Not one = True And Not two = True And Not three = False Then
> > MsgBox "One or more of the 3 conditions are false"
> > End If
> > End Sub
> >
> > Dan Thompson
> >

>
>
>

 
Reply With Quote
 
Dan Thompson
Guest
Posts: n/a
 
      25th May 2009
Hey JLGWhiz
This is what I am trying to accomplish only I would like to do it in the
shortest amount of code as possible. I think my code is a little long for
this macro. here is the code

Sub test()
Dim one As Boolean, two As Boolean, three As Boolean
Dim Count As Integer
one = True
two = True
three = True


For Count = 1 To 4
If Count = 1 Then one = False
If Count = 2 Then two = False
If Count = 3 Then three = False
MsgBox "Count # " & Count
If one = False Or two = False Or three = False Then
MsgBox "One or more of the 3 conditions are false"
End If
If one And two And three = True Then
MsgBox "All of the 3 conditions are true"
End If
one = True
two = True
three = True
Next Count
End Sub


"JLGWhiz" wrote:

> Also:
>
> If Not one = True Or Not two = True Or Not three = False Then
> MsgBox "One or more of the 3 conditions are false"
> End If
>
>
>
> "Dan Thompson" <(E-Mail Removed)> wrote in message
> news:B43C158A-196E-46BC-9B27-(E-Mail Removed)...
> > The following code should pop up a message box but it is not doing so
> > where
> > am I going wrong ?
> >
> >
> > Sub test()
> > Dim one As Boolean, two As Boolean, three As Boolean
> > one = True
> > two = True
> > three = False
> >
> > If Not one = True And Not two = True And Not three = False Then
> > MsgBox "One or more of the 3 conditions are false"
> > End If
> > End Sub
> >
> > Dan Thompson
> >

>
>
>

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      25th May 2009
I get all screwed around with the logic tables and have to use trial and
error when I get into testing multiple boolean results. Here is a site that
explains where the problem comes from.

http://www.rwc.uc.edu/koehler/comath/21.html

The If statement has to capture the condition so that the result is true to
make the command line execute, even if the result is negative. This means
that you can check to see if it Is, or you can check to see if it is Not,
but you cannot check to see if it is maybe. So when you use the And or the
Or operators in the criteria line of the If statement you need to be aware
of what the logic is doing in evaluating the statement.



"Dan Thompson" <(E-Mail Removed)> wrote in message
news:E24ACD38-68AC-4E03-B1C0-(E-Mail Removed)...
> Hey JLGWhiz
> This is what I am trying to accomplish only I would like to do it in the
> shortest amount of code as possible. I think my code is a little long for
> this macro. here is the code
>
> Sub test()
> Dim one As Boolean, two As Boolean, three As Boolean
> Dim Count As Integer
> one = True
> two = True
> three = True
>
>
> For Count = 1 To 4
> If Count = 1 Then one = False
> If Count = 2 Then two = False
> If Count = 3 Then three = False
> MsgBox "Count # " & Count
> If one = False Or two = False Or three = False Then
> MsgBox "One or more of the 3 conditions are false"
> End If
> If one And two And three = True Then
> MsgBox "All of the 3 conditions are true"
> End If
> one = True
> two = True
> three = True
> Next Count
> End Sub
>
>
> "JLGWhiz" wrote:
>
>> Also:
>>
>> If Not one = True Or Not two = True Or Not three = False Then
>> MsgBox "One or more of the 3 conditions are false"
>> End If
>>
>>
>>
>> "Dan Thompson" <(E-Mail Removed)> wrote in message
>> news:B43C158A-196E-46BC-9B27-(E-Mail Removed)...
>> > The following code should pop up a message box but it is not doing so
>> > where
>> > am I going wrong ?
>> >
>> >
>> > Sub test()
>> > Dim one As Boolean, two As Boolean, three As Boolean
>> > one = True
>> > two = True
>> > three = False
>> >
>> > If Not one = True And Not two = True And Not three = False Then
>> > MsgBox "One or more of the 3 conditions are false"
>> > End If
>> > End Sub
>> >
>> > Dan Thompson
>> >

>>
>>
>>



 
Reply With Quote
 
Dan Thompson
Guest
Posts: n/a
 
      25th May 2009
Thanks for the advice I fixed the problem and I realize it was to do with the
order of logic in my code. I understand now that however you present your
condition in an "IF" statement the "IF" statement always hast to evaluate to
be true in order for VBA to execute the code in the "IF" statement.
Bob = 42
IF Bob = 42 Then MsgBox "Happy Birthday" (Condition executes because it is
TRUE that Bob is 42)
Bob =35
IF Not Bob = 42 Then Exit Sub (Condition executes because it is TRUE that
Bob is NOT 42)
Both If statements execute because they are evaluated to be true
Right ??

on the other hand
Bob = 42
IF Not Bob = 42 Then Exit Sub (Condition will not execute because it is
FALSE and the "IF" statement will only execute code when the "IF" statement
returns true)

I think I got it


btw I looked at that link you sent me and it just made my head spin lol
too deep for me

Dan Thompson

"JLGWhiz" wrote:

> I get all screwed around with the logic tables and have to use trial and
> error when I get into testing multiple boolean results. Here is a site that
> explains where the problem comes from.
>
> http://www.rwc.uc.edu/koehler/comath/21.html
>
> The If statement has to capture the condition so that the result is true to
> make the command line execute, even if the result is negative. This means
> that you can check to see if it Is, or you can check to see if it is Not,
> but you cannot check to see if it is maybe. So when you use the And or the
> Or operators in the criteria line of the If statement you need to be aware
> of what the logic is doing in evaluating the statement.
>
>
>
> "Dan Thompson" <(E-Mail Removed)> wrote in message
> news:E24ACD38-68AC-4E03-B1C0-(E-Mail Removed)...
> > Hey JLGWhiz
> > This is what I am trying to accomplish only I would like to do it in the
> > shortest amount of code as possible. I think my code is a little long for
> > this macro. here is the code
> >
> > Sub test()
> > Dim one As Boolean, two As Boolean, three As Boolean
> > Dim Count As Integer
> > one = True
> > two = True
> > three = True
> >
> >
> > For Count = 1 To 4
> > If Count = 1 Then one = False
> > If Count = 2 Then two = False
> > If Count = 3 Then three = False
> > MsgBox "Count # " & Count
> > If one = False Or two = False Or three = False Then
> > MsgBox "One or more of the 3 conditions are false"
> > End If
> > If one And two And three = True Then
> > MsgBox "All of the 3 conditions are true"
> > End If
> > one = True
> > two = True
> > three = True
> > Next Count
> > End Sub
> >
> >
> > "JLGWhiz" wrote:
> >
> >> Also:
> >>
> >> If Not one = True Or Not two = True Or Not three = False Then
> >> MsgBox "One or more of the 3 conditions are false"
> >> End If
> >>
> >>
> >>
> >> "Dan Thompson" <(E-Mail Removed)> wrote in message
> >> news:B43C158A-196E-46BC-9B27-(E-Mail Removed)...
> >> > The following code should pop up a message box but it is not doing so
> >> > where
> >> > am I going wrong ?
> >> >
> >> >
> >> > Sub test()
> >> > Dim one As Boolean, two As Boolean, three As Boolean
> >> > one = True
> >> > two = True
> >> > three = False
> >> >
> >> > If Not one = True And Not two = True And Not three = False Then
> >> > MsgBox "One or more of the 3 conditions are false"
> >> > End If
> >> > End Sub
> >> >
> >> > Dan Thompson
> >> >
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      26th May 2009
Here is a different way to track your "true" conditions...

Sub Test()
Dim Count As Long
Dim TestCondition As String
TestCondition = String(3, "X") & " "
For Count = 1 To 4
MsgBox "Count #" & Count
MsgBox "Number of 'True' conditions is " & _
Len(Replace(TestCondition, " ", ""))
Mid(TestCondition, Count) = " "
Next Count
End Sub

--
Rick (MVP - Excel)


"Dan Thompson" <(E-Mail Removed)> wrote in message
news:E24ACD38-68AC-4E03-B1C0-(E-Mail Removed)...
> Hey JLGWhiz
> This is what I am trying to accomplish only I would like to do it in the
> shortest amount of code as possible. I think my code is a little long for
> this macro. here is the code
>
> Sub test()
> Dim one As Boolean, two As Boolean, three As Boolean
> Dim Count As Integer
> one = True
> two = True
> three = True
>
>
> For Count = 1 To 4
> If Count = 1 Then one = False
> If Count = 2 Then two = False
> If Count = 3 Then three = False
> MsgBox "Count # " & Count
> If one = False Or two = False Or three = False Then
> MsgBox "One or more of the 3 conditions are false"
> End If
> If one And two And three = True Then
> MsgBox "All of the 3 conditions are true"
> End If
> one = True
> two = True
> three = True
> Next Count
> End Sub
>
>
> "JLGWhiz" wrote:
>
>> Also:
>>
>> If Not one = True Or Not two = True Or Not three = False Then
>> MsgBox "One or more of the 3 conditions are false"
>> End If
>>
>>
>>
>> "Dan Thompson" <(E-Mail Removed)> wrote in message
>> news:B43C158A-196E-46BC-9B27-(E-Mail Removed)...
>> > The following code should pop up a message box but it is not doing so
>> > where
>> > am I going wrong ?
>> >
>> >
>> > Sub test()
>> > Dim one As Boolean, two As Boolean, three As Boolean
>> > one = True
>> > two = True
>> > three = False
>> >
>> > If Not one = True And Not two = True And Not three = False Then
>> > MsgBox "One or more of the 3 conditions are false"
>> > End If
>> > End Sub
>> >
>> > Dan Thompson
>> >

>>
>>
>>


 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      26th May 2009
You got the basic part. That is what counts. And it is all in the logic.


"Dan Thompson" <(E-Mail Removed)> wrote in message
news:A7712D63-59F4-4EFB-B194-(E-Mail Removed)...
> Thanks for the advice I fixed the problem and I realize it was to do with
> the
> order of logic in my code. I understand now that however you present your
> condition in an "IF" statement the "IF" statement always hast to evaluate
> to
> be true in order for VBA to execute the code in the "IF" statement.
> Bob = 42
> IF Bob = 42 Then MsgBox "Happy Birthday" (Condition executes because it
> is
> TRUE that Bob is 42)
> Bob =35
> IF Not Bob = 42 Then Exit Sub (Condition executes because it is TRUE that
> Bob is NOT 42)
> Both If statements execute because they are evaluated to be true
> Right ??
>
> on the other hand
> Bob = 42
> IF Not Bob = 42 Then Exit Sub (Condition will not execute because it is
> FALSE and the "IF" statement will only execute code when the "IF"
> statement
> returns true)
>
> I think I got it
>
>
> btw I looked at that link you sent me and it just made my head spin lol
> too deep for me
>
> Dan Thompson
>
> "JLGWhiz" wrote:
>
>> I get all screwed around with the logic tables and have to use trial and
>> error when I get into testing multiple boolean results. Here is a site
>> that
>> explains where the problem comes from.
>>
>> http://www.rwc.uc.edu/koehler/comath/21.html
>>
>> The If statement has to capture the condition so that the result is true
>> to
>> make the command line execute, even if the result is negative. This
>> means
>> that you can check to see if it Is, or you can check to see if it is Not,
>> but you cannot check to see if it is maybe. So when you use the And or
>> the
>> Or operators in the criteria line of the If statement you need to be
>> aware
>> of what the logic is doing in evaluating the statement.
>>
>>
>>
>> "Dan Thompson" <(E-Mail Removed)> wrote in message
>> news:E24ACD38-68AC-4E03-B1C0-(E-Mail Removed)...
>> > Hey JLGWhiz
>> > This is what I am trying to accomplish only I would like to do it in
>> > the
>> > shortest amount of code as possible. I think my code is a little long
>> > for
>> > this macro. here is the code
>> >
>> > Sub test()
>> > Dim one As Boolean, two As Boolean, three As Boolean
>> > Dim Count As Integer
>> > one = True
>> > two = True
>> > three = True
>> >
>> >
>> > For Count = 1 To 4
>> > If Count = 1 Then one = False
>> > If Count = 2 Then two = False
>> > If Count = 3 Then three = False
>> > MsgBox "Count # " & Count
>> > If one = False Or two = False Or three = False Then
>> > MsgBox "One or more of the 3 conditions are false"
>> > End If
>> > If one And two And three = True Then
>> > MsgBox "All of the 3 conditions are true"
>> > End If
>> > one = True
>> > two = True
>> > three = True
>> > Next Count
>> > End Sub
>> >
>> >
>> > "JLGWhiz" wrote:
>> >
>> >> Also:
>> >>
>> >> If Not one = True Or Not two = True Or Not three = False Then
>> >> MsgBox "One or more of the 3 conditions are false"
>> >> End If
>> >>
>> >>
>> >>
>> >> "Dan Thompson" <(E-Mail Removed)> wrote in
>> >> message
>> >> news:B43C158A-196E-46BC-9B27-(E-Mail Removed)...
>> >> > The following code should pop up a message box but it is not doing
>> >> > so
>> >> > where
>> >> > am I going wrong ?
>> >> >
>> >> >
>> >> > Sub test()
>> >> > Dim one As Boolean, two As Boolean, three As Boolean
>> >> > one = True
>> >> > two = True
>> >> > three = False
>> >> >
>> >> > If Not one = True And Not two = True And Not three = False Then
>> >> > MsgBox "One or more of the 3 conditions are false"
>> >> > End If
>> >> > End Sub
>> >> >
>> >> > Dan Thompson
>> >> >
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpected Result with "If" Logic/Conditional Statement =?Utf-8?B?QSBOZWVkIHRvIEV4Y2Vs?= Microsoft Excel Misc 4 5th Jul 2007 04:40 PM
How to write this "between dates" statement properly? vavroom@gmail.com Microsoft Access 3 13th Sep 2006 05:25 AM
"My Computer" "Address Bar" USB's not working properly =?Utf-8?B?cGxheXRpbGl0aHJ0eg==?= Windows XP Performance 4 14th Mar 2006 04:48 AM
"properties" says that "This device is working properly".... =?Utf-8?B?U2ltb24=?= Windows XP Help 3 20th May 2005 04:24 PM
Using "If" conditional statement with an "Include" page Don Goldberg Microsoft Frontpage 1 9th Jul 2003 07:37 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:57 PM.