Consulting

Results 1 to 7 of 7

Thread: Effect of "Else:"

  1. #1

    Effect of "Else:"

    I have the code:
    If "A" = "B" _
        Then Msgbox "A=B" _
         Else Msgbox "A<>B"
    then I decide to add a line:
    I
    f "A" = "B" _
       Then 
       Msgbox "A=B"
       Msgbox "More stuff"
       Else: Msgbox "A<>B"
       End If
    Because I forgot to move the Msgbox following the Else to the next line, VBE adds a colon following the Else.
    My question is, what is the effect of the added colon? I tried to set up a test to determine the answer, but everything I tried indicated that the colon has no effect.
    I'm missing something, but just can't figure it out. It looks like the Else has been converted to a label.

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Cyberdude

    I think the main effect of the colon is to make the code harder to understand and the flow of the code harder to follow.

  3. #3
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    Hi,

    You went from single line syntax to block form syntax. Therefore the code following the Else and before the End If needed to be converted to block form. The colon does this.
    You could have stay in single line form by placing a colon between your 2 msgbox statements.

    If "A" = "A" Then MsgBox "A=A":  MsgBox "More stuff" Else MsgBox "A<>A"
    I'm with Norie on this one though, I find using and then later reading colons to terminate multiple commands on a single line confusing.
    Cheers
    Andy

  4. #4
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Cyber,

    Effectively, the colon means "end of line" ... so whatever is written after a colon COULD (if you choose) be written on a new line.

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    Well, that explains why my tests told me nothing. Still, one wonders why the VBE writers chose to add the colon under these circumstances. Why not trigger an error??
    Thanks, gang.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Cyberdude
    Well, that explains why my tests told me nothing. Still, one wonders why the VBE writers chose to add the colon under these circumstances. Why not trigger an error??
    Thanks, gang.
    Because it is forgiving and is trying to work with you.

    Similalrly, if you type Endif, it converts it to End If, and adds a quotes at the end of the line if there is one missing,

    It's a feature, quite a useful one for once.

  7. #7
    Hard to believe, xld, but you're probably right.
    Thanx for the insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •