+ Reply to Thread
Results 1 to 16 of 16

variant comparisions fail

  1. #1
    Registered User
    Join Date
    12-05-2019
    Location
    London, Englind
    MS-Off Ver
    excel 1910
    Posts
    8

    variant comparisions fail

    Trying to compare temp_i and i. They are both 1. The compare says they are different. Why? I suspect this has
    something to do with variants. Thank you.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,401

    Re: variant comparisions fail

    Quote Originally Posted by hughy View Post
    Trying to compare temp_i and i. They are both 1. The compare says they are different. Why? I suspect this has
    something to do with variants. Thank you.

    Please Login or Register  to view this content.
    I am not sure what you are attempting with your code, but after you load minus one into the temp_v variable, you overwrite with CInt(i) and since i equals one, CInt(i) also equals one and, hence, you remove the minus one you placed in temp_v and replace it with (plus) one. That is why later on i and temp_v both equal one.
    Last edited by Rick Rothstein; 12-05-2019 at 06:48 PM.

  3. #3
    Registered User
    Join Date
    12-05-2019
    Location
    London, Englind
    MS-Off Ver
    excel 1910
    Posts
    8

    Re: variant comparisions fail

    They should both be one, but I always get this message. It says they are not both 1. (or rather the don't equal each other)

    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,401

    Re: variant comparisions fail

    Quote Originally Posted by hughy View Post
    They should both be one, but I always get this message. It says they are not both 1. (or rather the don't equal each other)

    Please Login or Register  to view this content.
    I do not get that message when I run the code you posted (once I remove the Exit Do since you did not post a Do..Loop structure).

  5. #5
    Registered User
    Join Date
    12-05-2019
    Location
    London, Englind
    MS-Off Ver
    excel 1910
    Posts
    8

    Re: variant comparisions fail

    Here's my experimental code as is. This should run as a function. I must be doing something
    bad in here. Thanks for helping.
    Please Login or Register  to view this content.

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,401

    Re: variant comparisions fail

    I still do not see what problem you are trying to convey. In your code, this line...

    temp-v = -1

    is doing nothing since the next line of code overwrites it with, basically, the value of i (CInt does nothing when the value passed into it is already an interger)... so i and temp_v will always display the same value in that last message box.

  7. #7
    Registered User
    Join Date
    12-05-2019
    Location
    London, Englind
    MS-Off Ver
    excel 1910
    Posts
    8

    Re: variant comparisions fail

    It's experimental. My question concerns only the result of the <> test which says the two variants are not equal.

    The -1 is an artifact. It used to be "dog". I was reading about the variant data type and they mentioned initial variant
    assignments usually set the type.

    Is there some reason my <> fails?

    Sorry if this was not clear. My bad. Thanks for helping.

  8. #8
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,401

    Re: variant comparisions fail

    Quote Originally Posted by hughy View Post
    My question concerns only the result of the <> test which says the two variants are not equal.

    Is there some reason my <> fails?
    It is not failing... the Exit Do inside that If block is not executing because the values are, in fact, equal. You can see this by placing a Breakpoint on that Exit Do and seeing that execution never stops on it.

  9. #9
    Registered User
    Join Date
    12-05-2019
    Location
    London, Englind
    MS-Off Ver
    excel 1910
    Posts
    8

    Re: variant comparisions fail

    Good. It should work but doesn't here. It always says they are unequal. There must be something going here I don't see. Thanks for your help.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,464

    Re: variant comparisions fail

    Quote Originally Posted by hughy View Post
    Trying to compare temp_i and i. They are both 1. The compare says they are different. Why? I suspect this has
    something to do with variants.
    They are the same.

    Is what case do you wnat the function to return True/False?

  11. #11
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,401

    Re: variant comparisions fail

    Quote Originally Posted by hughy View Post
    Good. It should work but doesn't here. It always says they are unequal. There must be something going here I don't see. Thanks for your help.
    Are you saying this line of code...

    MsgBox ("temp is " & temp_v & ", and i is " & i)

    is displaying unequal values for i and temp_v? If so, show us the exact line of code you are using to call your function.

  12. #12
    Registered User
    Join Date
    12-05-2019
    Location
    London, Englind
    MS-Off Ver
    excel 1910
    Posts
    8

    Re: variant comparisions fail

    First print says values are equal. Even so, "why am I here" says they are not.

    Please Login or Register  to view this content.

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,464

    Re: variant comparisions fail

    How are you calling the function?

    Aren't you testing the string value like
    Please Login or Register  to view this content.
    ?
    otherwise, no idea.

  14. #14
    Registered User
    Join Date
    12-05-2019
    Location
    London, Englind
    MS-Off Ver
    excel 1910
    Posts
    8

    Re: variant comparisions fail

    I changed

    dim temp_v as variant

    to

    dim temp_v as integer

    and everything is now ok. Why the comparison fails is beyond me. Thanks for all help. It's still a mystery but the code now works.

  15. #15
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent SAC
    Posts
    8,885

    Re: variant comparisions fail

    How are you calling the function? If you pass "1" rather than 1, you would get the behaviour you are describing.
    Rory

  16. #16
    Registered User
    Join Date
    12-05-2019
    Location
    London, Englind
    MS-Off Ver
    excel 1910
    Posts
    8

    Re: variant comparisions fail

    That must be it. I went back and changed temp_v to a variant and "cast" temp_v this way

    temp_v = CInt(i)

    if i <> CInt(temp_v) then ....

    The above appears to "make" the if compare integers. Thanks all. This thread is closed. Thanks to all.

+ 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. How to make a cell display "FAIL" when range of cells is FAIL
    By crazychile in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-18-2017, 04:13 PM
  2. [SOLVED] A variable defined as variant or variant array
    By billj in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-14-2016, 05:35 PM
  3. Does setting a variant acting as an array equal to "" accomplish same as "Erase" variant?
    By dmasters4919 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2014, 05:59 PM
  4. [SOLVED] Need a formula for pass/fail based on no. of Y answers but automatically fail if 3a is N
    By Researcher1111 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 09-15-2013, 10:20 PM
  5. Cell comparisions
    By preveo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-22-2012, 04:57 PM
  6. [SOLVED] Implementing Binary Comparisions for a class objects
    By Robert Mulroney in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-27-2005, 07:00 PM

Tags for this Thread

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