Results 1 to 7 of 7
  1. #1
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Weird IF Structure (Excel 2003)

    Reference to Post: <post#=498,943>post 498,943</post#>

    I noticed this weird if structure...can anyone fill me in to what it does...??

    Look at the sample below...what is the significance to the ( ":") colon...I can now assign two (or more?) actions to the true (or false) statments!!
    Sub Test()
    If Range("A1").Value = 1 Then
    Range("B1").Value = 1: Range("C1").Value = 0: End If
    Range("A2").Select
    End Sub

    When would you use this, and is my assumptions correct?

    Tx
    Regards,
    Rudi

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Weird IF Structure (Excel 2003)

    Range("B1").Value = 1: Range("C1").Value = 0: End If
    is equivalent to:
    Range("B1").Value = 1
    Range("C1").Value = 0
    End If

    The space underscore ( _) allows wrapping a long line, the colon ( : ) allows combining short lines.

    [Personally, I prefer the separated code for readability]

    Steve

  3. #3
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Weird IF Structure (Excel 2003)

    Cheers : Its the first time I ever have seen that : Tx for the speedy reply : Maybe we can make use of this in Word : Type some great letters and save some space!!! <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>
    Regards,
    Rudi

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Weird IF Structure (Excel 2003)

    It's a left-over from very early versions of BASIC, when it could be very useful to save a few bytes of storage space (multiple instructions on one line take up less space than the same instructions on separate lines). Now that available memory and disk space are much larger, there is no need for it any more, and because it makes code harder to read, it's best avoided.

  5. #5
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Weird IF Structure (Excel 2003)

    True - I prefer the "structured" code anyways...
    Tx
    Regards,
    Rudi

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Weird IF Structure (Excel 2003)

    FWIW, the only time I use that syntax is if I am running a short (typically 3 line loop) routine from the Immediate window rather than bothering to write a sub.
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Weird IF Structure (Excel 2003)

    Actually, now that you mention it, this type of "1 liner" structure really suits the immediate window...which works on that type of basis....tx for that comment!
    Regards,
    Rudi

Posting Permissions

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