+ Reply to Thread
Results 1 to 7 of 7

Short circuiting logical expressions?

  1. #1
    Registered User
    Join Date
    06-07-2007
    Location
    Sweden
    MS-Off Ver
    Excel 2003
    Posts
    15

    Short circuiting logical expressions?

    Hi everyone!

    I have a problem with a part of my program. I would like to execute the second expression in this statement:
    If AndAlso(ActiveCell.Offset(i - 1, 2) <> "", DateValue(ActiveCell.Offset (i - 1, 2)) < DateValue(Today)) Then
    ActiveCell.Offset(i - 1, -7).Value = "4"
    ONLY if the first expression is not true. Since excel seems to execute both statements in an AND command, I wrote my own AndAlso function:
    Public Function AndAlso(expr1 As Boolean, expr2 As Boolean) As Boolean
    If expr1 Then
    If expr2 Then
    AndAlso = True
    Else
    AndAlso = False
    End If
    Else
    AndAlso = False
    End If
    End Function
    (Sorry for the bad indent!) It doesn't look so nice, so I am figuring that there must be better ways. Also I still get an error message from the DateValue-execution when the cell is empty. Anyone has any ideas?

  2. #2
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326
    Hi, please take a moment to read forum rules about wrapping your code with tags.

  3. #3
    Registered User
    Join Date
    06-07-2007
    Location
    Sweden
    MS-Off Ver
    Excel 2003
    Posts
    15
    Oh right, sorry, here is a new try to post the code:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326
    Ok, thx for adapting. You didn't have to make a new post, just edit your original one, and adapt.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Your function receives two logical arguments. The way they got from being expressions to being Booleans is via evaluation. If one of them evaluates to an error, AndAlso never executes.

    Skip AndAlso, but use the same construct as you did in AndAlso.

    And also (), instead of
    Please Login or Register  to view this content.
    use
    Please Login or Register  to view this content.
    Last edited by shg; 11-03-2008 at 05:36 PM.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    06-07-2007
    Location
    Sweden
    MS-Off Ver
    Excel 2003
    Posts
    15

    That gives problem with the if statements instead ...

    Thanks for your reply! I removed the function but now there is a different error due to the nested ifs. See, I have three columns (and multiple rows) of information to look through and the cells can be either empty or contain a date, so I have to check each cell first and then calculate if the date has passed or not. If it has passed, then it should change to the next row.

    Although the sub continues to check the other columns too and replaces the result with the number of the last non-empty cell. Since an "Exit For" could not solve - what can?

    Please Login or Register  to view this content.
    Thanks in advance for all help possible! This is really tricky for me!

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    I can't tell what you're trying to do. Maybe this:
    Please Login or Register  to view this content.
    Also, if the cells in question contain actual dates (as opposed to text that looks like dates), I'd change the coode to look more like this:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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