+ Reply to Thread
Results 1 to 4 of 4

How to write one single line of VBA code to return TRUE/FALSE for And/Or statement?

  1. #1
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    801

    How to write one single line of VBA code to return TRUE/FALSE for And/Or statement?

    Statement #1: (A1=1 AND (A2 <> 3 OR A3 > 5)) OR ((A4>9 OR A5 <= 8) AND A6 > 20)

    Statement #2: OR( AND(A1=1, OR(A2 <>3, A3 >5)), AND(OR(A4>9, A5 <=8),A6 >20) )

    Please Login or Register  to view this content.

    Attached Photo
    : Yellow cells are filled with numbers.

    How to write one single line of code for RED portion of code? The code can be in either format of Statement 1 or Statement 2.

    E.g. If A1=35, then A1=1 is False. Or I can write separate code to figure out if A1 = 1 is True or False, then replace A1=1 with True/False. Red portion can also be something like(if I decide to break above Red portion of code into two steps. First step: figuring out True/False for each substatement):
    (False AND (True OR True)) OR ((False OR True) AND True)

    Thanks.
    Attached Images Attached Images
    Last edited by VAer; 08-22-2018 at 12:51 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,673

    Re: How to write one single line of VBA code to return TRUE/FALSE for And/Or statement?

    Statement #1 and Statement #2 are already valid VBA logical expressions that you can use in your code.

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    801

    Re: How to write one single line of VBA code to return TRUE/FALSE for And/Or statement?

    Quote Originally Posted by 6StringJazzer View Post
    Statement #1 and Statement #2 are already valid VBA logical expressions that you can use in your code.

    Please Login or Register  to view this content.
    Thanks. Do you know why the string (below code) does not work? For some reason, people need to write syntax with prefix (prefix means something, I would briefly explain a little, e.g. C.A1 means Current Year A1, P.A1 means Previous year A1): C.A2 > 5 Or P.A5 <> 7

    Prefix determines where to pull data from, BUT I cannot use prefix in IF statement directly, Prefix cannot even be used as variable, I can not write Dim C.A1 As Integer

    So before writing If....Then statement, I need my program to look at each sub-statement ( C.A2 > 5 & P.A5 <> 7) and determine if they are True or False

    First step: I write Str = "C.A2 > 5 Or P.A5 <> 7"
    Second Step: I look at current year table and determine if C.A2 > 5 is True or False, if it is True, I will substitute it with True. Str = "True Or P.A5 <> 7"
    Third Step: I look at previous year table and determine if P.A5 <> 7 is True or False, if it is False, I will substitute it with False. Str ="True Or False"

    But at this point, I only have variable Str , but If Str Then cannot be compiled.

    However, If True Or False Then can be compiled. The issue is I only have the variable Str, I could not find a way to write If True Or False Then directly, If True Or False Then is from multiple steps of substitutions.

    How can I go around the issue? In other words, how to write If C.A2 > 5 Or P.A5 <> 7 Then ? Maybe I should write Dim Str As Boolean , but I still cannot find a way to write code to figure out if it is true or false for C.A2 > 5 Or P.A5 <> 7

    C.A2 > 5 Or P.A5 <> 7 is what I have from other party, I cannot change the way other people to write the statement, my role is to write code and determine if the statement is True or False.

    Please Login or Register  to view this content.
    Last edited by VAer; 08-22-2018 at 09:48 PM.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,673

    Re: How to write one single line of VBA code to return TRUE/FALSE for And/Or statement?

    I think you are suffering from some confusion about how to write logical evaluation in code. First I'll explain why the following code doesn't work:

    Please Login or Register  to view this content.
    An If statement determines the value of a Boolean expression, and if that expression evaluates to True then it does the Then part. You have defined Str as a String. A String is not a Boolean expression. So your code doesn't work. It doesn't matter that you have chosen to assign the string "True of False" which looks like a logical expression, but it's still a String. The example below shows the difference:

    Please Login or Register  to view this content.
    The first MsgBox will show the string
    True Or False

    The second MsgBox will show the Boolean value
    True
    because the logical expression True Or False evaluates to True.


    Quote Originally Posted by VAer View Post
    First step: I write Str = "C.A2 > 5 Or P.A5 <> 7"
    Second Step: I look at current year table and determine if C.A2 > 5 is True or False, if it is True, I will substitute it with True. Str = "True Or P.A5 <> 7"
    Third Step: I look at previous year table and determine if P.A5 <> 7 is True or False, if it is False, I will substitute it with False. Str ="True Or False"
    You cannot build a logical expression using strings. You are also making this more complicated that it needs to be. There is no reason to build up a single variable like that. You ask, "In other words, how to write If C.A2 > 5 Or P.A5 <> 7 Then ? " The answer is: You write it exactly like that!
    Please Login or Register  to view this content.
    It appears that C.A2 and P.A5 are just placeholders for some other variables, so this isn't real code. The example in your first post is also not real code. I can't give you a real code answer unless you provide real code to start with.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. If(And) Statement for sorting time stamp to return true or false value
    By engineerd in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-28-2017, 03:12 PM
  2. Replies: 1
    Last Post: 12-13-2016, 12:23 AM
  3. [SOLVED] Multiple ifs to return more than true or false statement
    By Vince1199s in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 03-08-2016, 01:12 PM
  4. How to return a value instead of TRUE/FALSE with IF,OR,AND statement
    By jgoat23 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-14-2014, 10:30 AM
  5. [SOLVED] VBA Code for Option Buttons to return YES or NO instead of TRUE or FALSE
    By mebanet in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-31-2013, 02:42 AM
  6. [SOLVED] how to write three macro code in single line ?
    By nur2544 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2013, 03:29 AM
  7. Replies: 9
    Last Post: 01-21-2013, 01:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1