+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 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
    Valued Forum Contributor
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2010
    Posts
    671

    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
    Valued Forum Contributor
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2010
    Posts
    671

    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
    Valued Forum Contributor
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2010
    Posts
    671

    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
    Valued Forum Contributor
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2010
    Posts
    671

    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
    Posts
    15,218

    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
    Valued Forum Contributor
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2010
    Posts
    671

    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
    Posts
    15,218

    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
    UK
    MS-Off Ver
    2010 primarily
    Posts
    3,807

    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
    Days when we raged, we flew off the page
    Such damage was done

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Thread Information

Users Browsing this Thread

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

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