+ Reply to Thread
Results 1 to 6 of 6

Unusual syntax in If statement

  1. #1
    Registered User
    Join Date
    09-03-2009
    Location
    Red Bank, NJ
    MS-Off Ver
    Excel 2010
    Posts
    55

    Unusual syntax in If statement

    Recently ran into the following syntax, which is unfamiliar to me:

    If (X And 1) <> 0 then
    Code
    End If

    X is an integer.

    Can someone explain what the parenthetic "And" does?

    Thank you.
    Last edited by avr5iron; 09-01-2016 at 02:06 PM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Unusual syntax in If statement

    This looks like an excel function, not a VBA.

  3. #3
    Registered User
    Join Date
    09-03-2009
    Location
    Red Bank, NJ
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Unusual syntax in If statement

    It is part of a FuzzyLookup function I found searching online. It is VBA, and it works perfectly. I just don't understand the parenthetic "And", within the if statement. The integer, "X", is an optional function argument (default = 3). Used to determine whether or not to execute a particular Sub, like...

    If (X And 1) <> 0 Then
    Alg1 Arg1, Arg2, Arg3
    End If

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Unusual syntax in If statement

    In short, it checks whether the number is odd or even :
    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  5. #5
    Registered User
    Join Date
    09-03-2009
    Location
    Red Bank, NJ
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Unusual syntax in If statement

    Wow. Wouldn't have guessed that. Thank you.

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Unusual syntax in If statement

    You are welcome, thanks for marking the thread as solved.

    If you want to dig a little deeper, here is the explanation :
    Please Login or Register  to view this content.
    For "AND" binary comparison :
    0 and 0 = 0
    0 and 1 = 0
    1 and 0 = 0
    1 and 1 = 1
    Only both value is 1 will yield 1


    Now check for 1 and 1 :
    1 and 1 = 1
    00000001
    00000001
    --------
    00000001

    Comparing first digit (the leftmost) :
    0 and 0 --> 0

    Comparing second digit (second from left) :
    0 and 0 --> 0

    ...

    Comparing last digit (first from right) :
    1 and 1 --> 1

    so the result is 00000001 in binary, which is number 1



    Now check for 2 and 1 :
    2 and 1 = 0
    00000010
    00000001
    ---------
    00000000

    Comparing first digit (the leftmost) :
    0 and 0 --> 0

    Comparing second digit (second from left) :
    0 and 0 --> 0

    Comparing third digit (third from left) :
    0 and 0 --> 0

    Comparing fourth digit (fourth from left) :
    0 and 0 --> 0

    Comparing fifth digit (fifth from left) :
    0 and 0 --> 0

    Comparing sixth digit (sixth from left) :
    0 and 0 --> 0

    Comparing seventh digit (seventh from left) :
    1 and 0 --> 0

    Comparing last digit (eightht from left) :
    0 and 1 --> 0

    so the result is 00000000 in binary, which is number 0

+ 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. [SOLVED] If then statement syntax and layout
    By stockgoblin42 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-01-2013, 09:57 AM
  2. [SOLVED] Proper syntax for an If statement with OR
    By publius190 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-26-2012, 10:00 AM
  3. IF AND statement syntax
    By jdeverich in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-23-2011, 03:16 AM
  4. Syntax question on If statement
    By Guinness85 in forum Excel General
    Replies: 2
    Last Post: 07-28-2011, 05:21 PM
  5. Help with syntax of if/and statement
    By Cubby in forum Excel General
    Replies: 3
    Last Post: 09-15-2006, 12:12 PM
  6. [SOLVED] Syntax for multiple if statement
    By AndyRoo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-03-2005, 08:25 PM
  7. help with syntax for if and statement
    By snsyg in forum Excel General
    Replies: 3
    Last Post: 10-04-2005, 11:25 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