Share via

assignment versus comparison

Anonymous
2011-03-02T18:11:08+00:00

I'm trying to get my head straight about similarities and differences between VBA and JavaScript/PHP.

In the latter there's a strict distinction between assignment (using '=') and comparison (using '=='). I don't see any such distinction in my VBA sources.

Am I correct in concluding that in VBA I don't need to make the distinction and can just use '=' in both kinds of situations?

Thanks.

Microsoft 365 and Office | Install, redeem, activate | For home | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-03-02T21:13:10+00:00

I don't work in PHP, but from what you wrote, I think a clarification is needed. In VBA, assignments cannot be embedded within other function calls... they must be done is a stand-alone statement (that is how come the equal sign can serve two purposes... how it is being used determines how it is implemented). The way you would write your PHP example wouls be like this...

r = "dog"

If Len(r) > 0 Then

  ' do something

End If

or, you could write the If test this way...

If r <> "" Then

but the Len method is faster in VBA (due to the way strings are stored in it). Oh, I guess I should explain Mike's example. In this statement...

r = "this" = "THIS"

the first equal sign is an assigment (because there is a variable on the left of it) and the second equal sign is a logical comparison operator because an assignment cannot take place from that position in the code statement.

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-03-02T21:39:40+00:00

     In VBA, assignments cannot be embedded within other function calls... they must be done is a stand-alone statement

    That's the differentiator I was looking for.

    Thanks.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-03-02T19:25:35+00:00

    So in PHP if I write

       if(r="dog")

       {  //do something },

    this will always do something because PHP takes the condition as an assignment and so it's always true, but VBA can somehow tell (from context?) that that's not what's wanted:

      if r="dog" then

         'do something

      end if

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-03-02T18:45:45+00:00

    Hi,

    In VBA then the single = serves as both assignment and comparitor but is case sensitive in the latter case. This for example evaluates as FALSE

    r = "this" = "THIS"

    Was this answer helpful?

    0 comments No comments