Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. VBA GoTo Hell

VBA GoTo Hell

Scheduled Pinned Locked Moved The Weird and The Wonderful
17 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Xiangyang Liu

    Is your anger mainly directed to VBA or to your colleague's code? What if your colleague wrote the following instead?

    For counter = avgfirstrow To avglastrow
    cellvalue = Sheets(1).Cells(counter, 3).Value
    If cellvalue < 0 Or cellvalue > 99 Then entries = entries - 1
    Else totalsum = totalsum + cellvalue
    Next

    The reason I ask is many people seem to be happy with denouncing anything with VB in it, not realizing that it is possible to write good code in VB/VBA, just like it is possible to write bad code in C++/C#. Thanks.

    My .NET Business Application Framework My Home Page

    G Offline
    G Offline
    GuyThiebaut
    wrote on last edited by
    #3

    Xiangyang Liu wrote:

    Is your anger mainly directed to VBA or to your colleague's code?

    My colleagues lack of coding proficiency(What a Scrooge I am and it's Chistmas tomorrow). Unfortunately I have not been supervising his coding enough. It's a tricky area as happy coders make better coders - so I try to have a light touch when it comes to others code. Although I do like to have a good old moan on CodeProject occasionally.

    Xiangyang Liu wrote:

    it is possible to write good code in VB/VBA, just like it is possible to write bad code in C++/C#.

    I agree. My experience is that VBA encourages bad techniques and habits. To code in C# you have to have a relatively good understanding of programming methodology(am I wrong?). You can throw anyone at VBA and it will throw something back up.

    Xiangyang Liu wrote:

    What if your colleague wrote the following instead?

    I would have had nothing to complain about except for the formatting(there I go again). Merry Christmas :) Guy

    You always pass failure on the way to success.
    V 1 Reply Last reply
    0
    • X Xiangyang Liu

      Is your anger mainly directed to VBA or to your colleague's code? What if your colleague wrote the following instead?

      For counter = avgfirstrow To avglastrow
      cellvalue = Sheets(1).Cells(counter, 3).Value
      If cellvalue < 0 Or cellvalue > 99 Then entries = entries - 1
      Else totalsum = totalsum + cellvalue
      Next

      The reason I ask is many people seem to be happy with denouncing anything with VB in it, not realizing that it is possible to write good code in VB/VBA, just like it is possible to write bad code in C++/C#. Thanks.

      My .NET Business Application Framework My Home Page

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #4

      I agree with you. Is the average VB programmer who brought discredit on the language. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      [my articles]

      1 Reply Last reply
      0
      • X Xiangyang Liu

        Is your anger mainly directed to VBA or to your colleague's code? What if your colleague wrote the following instead?

        For counter = avgfirstrow To avglastrow
        cellvalue = Sheets(1).Cells(counter, 3).Value
        If cellvalue < 0 Or cellvalue > 99 Then entries = entries - 1
        Else totalsum = totalsum + cellvalue
        Next

        The reason I ask is many people seem to be happy with denouncing anything with VB in it, not realizing that it is possible to write good code in VB/VBA, just like it is possible to write bad code in C++/C#. Thanks.

        My .NET Business Application Framework My Home Page

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #5

        "Omit needless local variables." -- Strunk... if he'd taught programming (cellvalue in this case)

        G A 2 Replies Last reply
        0
        • P PIEBALDconsult

          "Omit needless local variables." -- Strunk... if he'd taught programming (cellvalue in this case)

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #6

          Thanks - I missed that one as well. Merry Christmas :)

          You always pass failure on the way to success.
          1 Reply Last reply
          0
          • X Xiangyang Liu

            Is your anger mainly directed to VBA or to your colleague's code? What if your colleague wrote the following instead?

            For counter = avgfirstrow To avglastrow
            cellvalue = Sheets(1).Cells(counter, 3).Value
            If cellvalue < 0 Or cellvalue > 99 Then entries = entries - 1
            Else totalsum = totalsum + cellvalue
            Next

            The reason I ask is many people seem to be happy with denouncing anything with VB in it, not realizing that it is possible to write good code in VB/VBA, just like it is possible to write bad code in C++/C#. Thanks.

            My .NET Business Application Framework My Home Page

            P Offline
            P Offline
            peterchen
            wrote on last edited by
            #7

            Xiangyang Liu wrote:

            that it is possible to write good code in VB/VBA, just like it is possible to write bad code in C++/C#.

            But it is easier to write bad code in C++/C#, than it is to write good code in VB/VBA! ;P

            We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
            My first real C# project | Linkify!| FoldWithUs! | sighist

            1 Reply Last reply
            0
            • X Xiangyang Liu

              Is your anger mainly directed to VBA or to your colleague's code? What if your colleague wrote the following instead?

              For counter = avgfirstrow To avglastrow
              cellvalue = Sheets(1).Cells(counter, 3).Value
              If cellvalue < 0 Or cellvalue > 99 Then entries = entries - 1
              Else totalsum = totalsum + cellvalue
              Next

              The reason I ask is many people seem to be happy with denouncing anything with VB in it, not realizing that it is possible to write good code in VB/VBA, just like it is possible to write bad code in C++/C#. Thanks.

              My .NET Business Application Framework My Home Page

              S Offline
              S Offline
              Scott Barbour
              wrote on last edited by
              #8

              Your code is in error. You cannot have the "Else" clause on a separate line if the "Then" clause is on the same line as the condition.

              For counter = avgfirstrow To avglastrow
              cellvalue = Sheets(1).Cells(counter, 3).Value
              If cellvalue < 0 or cellvalue > 99 Then
              entries = entries - 1
              Else
              totalsum = totalsum + cellvalue
              End If
              Next

              1 Reply Last reply
              0
              • P PIEBALDconsult

                "Omit needless local variables." -- Strunk... if he'd taught programming (cellvalue in this case)

                A Offline
                A Offline
                anony
                wrote on last edited by
                #9

                Actually his usage of a private variable was correct, because otherwords he would have to keep drilling down the object model to get the value in each conditional, and it's better to incur the performance overhead of allocating a new variable once than to keep traversing the object model more than once in a loop like that. "Real" VB (which is NOT the same as VBA) gives you the With statement which prevents the need for this additional variable. What he did actually makes the code more readable as well. Keep in mind that VBA is meant for simple macros, so there is really no horror in what he did, even with macros. Gotos are considered bad because they lead to spaghetti code smells in applications that have grown beyond a particular size, but used in a small macro situation they can greatly enhance readability if used properly. I'm not trying to wave the VB banner, I actually bailed on it quite some time ago only using it when I have to, simply because most good developers chose to focus on C#, thus it's easier to go with the flow and I like C-like syntaxes. But most VB-bigots are not as smart as they think they are and would feel quite stupid for making blanket statements about VB if they knew all the facts. I've seen really amazing, robust, well-performing apps written in both VB6 and VB.Net, and VB itself incorporates many elements of Pascal which is an excellent language. I do agree that the relative simplicity of getting started with VB led to an influx of bad programmers (many of which got flushed out in the dot com bust), or people who were uneducated in formal CS concepts or simply were not technical or motivated enough to make a career committment to good programming, but at the same time I've worked with some real idiots whose background was in Java or C++. I always thought Microsft screwed up by keeping the whole "BASIC" moniker... when they came out with .Net they should have just named it B++ or B# or something that made more sense, because despite a few syntax differences it is much closer to C# than it is to the original BASIC.

                G 1 Reply Last reply
                0
                • G GuyThiebaut

                  I found this piece of VeryBAd VBA code in an Excel spreadsheet I was amending today:

                  For counter = avgfirstrow To avglastrow
                      
                      cellvalue = Sheets(1).Cells(counter, 3).Value
                      If cellvalue < 0 Then GoTo ignore
                      If cellvalue > 99 Then GoTo ignore
                      
                      totalsum = totalsum + cellvalue
                      GoTo nextone
                  ignore:
                      entries = entries - 1
                  nextone:
                  
                  Next
                  

                  I can assure you my left eyebrow is still twitching. Gibber gibber.... :wtf: My only guess is that Satan must have been whispering into my colleague's ear at the time.

                  You always pass failure on the way to success.
                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #10

                  Yikes! Why not put entries = entries - 1 after Then :eek: Also just have one if statement :wtf:

                  "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                  1 Reply Last reply
                  0
                  • A anony

                    Actually his usage of a private variable was correct, because otherwords he would have to keep drilling down the object model to get the value in each conditional, and it's better to incur the performance overhead of allocating a new variable once than to keep traversing the object model more than once in a loop like that. "Real" VB (which is NOT the same as VBA) gives you the With statement which prevents the need for this additional variable. What he did actually makes the code more readable as well. Keep in mind that VBA is meant for simple macros, so there is really no horror in what he did, even with macros. Gotos are considered bad because they lead to spaghetti code smells in applications that have grown beyond a particular size, but used in a small macro situation they can greatly enhance readability if used properly. I'm not trying to wave the VB banner, I actually bailed on it quite some time ago only using it when I have to, simply because most good developers chose to focus on C#, thus it's easier to go with the flow and I like C-like syntaxes. But most VB-bigots are not as smart as they think they are and would feel quite stupid for making blanket statements about VB if they knew all the facts. I've seen really amazing, robust, well-performing apps written in both VB6 and VB.Net, and VB itself incorporates many elements of Pascal which is an excellent language. I do agree that the relative simplicity of getting started with VB led to an influx of bad programmers (many of which got flushed out in the dot com bust), or people who were uneducated in formal CS concepts or simply were not technical or motivated enough to make a career committment to good programming, but at the same time I've worked with some real idiots whose background was in Java or C++. I always thought Microsft screwed up by keeping the whole "BASIC" moniker... when they came out with .Net they should have just named it B++ or B# or something that made more sense, because despite a few syntax differences it is much closer to C# than it is to the original BASIC.

                    G Offline
                    G Offline
                    GuyThiebaut
                    wrote on last edited by
                    #11

                    Tipster wrote:

                    Keep in mind that VBA is meant for simple macros

                    Not in my programming experience. I have had to use VBA for some fairly sophisticated image manipulation in Word as well as using it extensively in Access and Excel.

                    Tipster wrote:

                    Gotos are considered bad... but used in a small macro situation they can greatly enhance readability if used properly

                    I'd like to see an example of that. Ok I have used goto's (Oh I know the shame of it :sigh:) but really they should never be used if there is a better alternative. When I was at University the only place a goto was ever allowed was in certain situations in COBOL. I was taught to program properly through PASCAL and I think many people new to programming would benefit from this sort of education - i.e. structured programming. Alright - rant over ;)

                    You always pass failure on the way to success.
                    1 Reply Last reply
                    0
                    • X Xiangyang Liu

                      Is your anger mainly directed to VBA or to your colleague's code? What if your colleague wrote the following instead?

                      For counter = avgfirstrow To avglastrow
                      cellvalue = Sheets(1).Cells(counter, 3).Value
                      If cellvalue < 0 Or cellvalue > 99 Then entries = entries - 1
                      Else totalsum = totalsum + cellvalue
                      Next

                      The reason I ask is many people seem to be happy with denouncing anything with VB in it, not realizing that it is possible to write good code in VB/VBA, just like it is possible to write bad code in C++/C#. Thanks.

                      My .NET Business Application Framework My Home Page

                      M Offline
                      M Offline
                      Matt Sollars
                      wrote on last edited by
                      #12

                      Where are the Goto statements in C++/C#??


                      Matt
                      (Find your own niche! This one's mine.)

                      G 1 Reply Last reply
                      0
                      • G GuyThiebaut

                        I found this piece of VeryBAd VBA code in an Excel spreadsheet I was amending today:

                        For counter = avgfirstrow To avglastrow
                            
                            cellvalue = Sheets(1).Cells(counter, 3).Value
                            If cellvalue < 0 Then GoTo ignore
                            If cellvalue > 99 Then GoTo ignore
                            
                            totalsum = totalsum + cellvalue
                            GoTo nextone
                        ignore:
                            entries = entries - 1
                        nextone:
                        
                        Next
                        

                        I can assure you my left eyebrow is still twitching. Gibber gibber.... :wtf: My only guess is that Satan must have been whispering into my colleague's ear at the time.

                        You always pass failure on the way to success.
                        J Offline
                        J Offline
                        jgrogan
                        wrote on last edited by
                        #13

                        Hmmm, that's pretty good compared to some of the code I've had to deal with :laugh:

                        GuyThiebaut wrote:

                        I found this piece of VeryBAd VBA code in an Excel spreadsheet I was amending today: For counter = avgfirstrow To avglastrow cellvalue = Sheets(1).Cells(counter, 3).Value If cellvalue < 0 Then GoTo ignore If cellvalue > 99 Then GoTo ignore totalsum = totalsum + cellvalue GoTo nextoneignore: entries = entries - 1nextone:Next I can assure you my left eyebrow is still twitching. Gibber gibber.... My only guess is that Satan must have been whispering into my colleague's ear at the time.

                        G 1 Reply Last reply
                        0
                        • J jgrogan

                          Hmmm, that's pretty good compared to some of the code I've had to deal with :laugh:

                          GuyThiebaut wrote:

                          I found this piece of VeryBAd VBA code in an Excel spreadsheet I was amending today: For counter = avgfirstrow To avglastrow cellvalue = Sheets(1).Cells(counter, 3).Value If cellvalue < 0 Then GoTo ignore If cellvalue > 99 Then GoTo ignore totalsum = totalsum + cellvalue GoTo nextoneignore: entries = entries - 1nextone:Next I can assure you my left eyebrow is still twitching. Gibber gibber.... My only guess is that Satan must have been whispering into my colleague's ear at the time.

                          G Offline
                          G Offline
                          GuyThiebaut
                          wrote on last edited by
                          #14

                          I guess it makes sense, and works, in it's own eccentric manner.:~

                          You always pass failure on the way to success.
                          1 Reply Last reply
                          0
                          • M Matt Sollars

                            Where are the Goto statements in C++/C#??


                            Matt
                            (Find your own niche! This one's mine.)

                            G Offline
                            G Offline
                            GuyThiebaut
                            wrote on last edited by
                            #15

                            Matt Sollars wrote:

                            Where are the Goto statements in C++/C#??

                            Surely that is an oxymoron ;)

                            You always pass failure on the way to success.
                            M 1 Reply Last reply
                            0
                            • G GuyThiebaut

                              Matt Sollars wrote:

                              Where are the Goto statements in C++/C#??

                              Surely that is an oxymoron ;)

                              You always pass failure on the way to success.
                              M Offline
                              M Offline
                              Matt Sollars
                              wrote on last edited by
                              #16

                              ;)


                              Matt
                              (Find your own niche! This one's mine.)

                              1 Reply Last reply
                              0
                              • G GuyThiebaut

                                Xiangyang Liu wrote:

                                Is your anger mainly directed to VBA or to your colleague's code?

                                My colleagues lack of coding proficiency(What a Scrooge I am and it's Chistmas tomorrow). Unfortunately I have not been supervising his coding enough. It's a tricky area as happy coders make better coders - so I try to have a light touch when it comes to others code. Although I do like to have a good old moan on CodeProject occasionally.

                                Xiangyang Liu wrote:

                                it is possible to write good code in VB/VBA, just like it is possible to write bad code in C++/C#.

                                I agree. My experience is that VBA encourages bad techniques and habits. To code in C# you have to have a relatively good understanding of programming methodology(am I wrong?). You can throw anyone at VBA and it will throw something back up.

                                Xiangyang Liu wrote:

                                What if your colleague wrote the following instead?

                                I would have had nothing to complain about except for the formatting(there I go again). Merry Christmas :) Guy

                                You always pass failure on the way to success.
                                V Offline
                                V Offline
                                Vasudevan Deepak Kumar
                                wrote on last edited by
                                #17

                                GuyThiebaut wrote:

                                To code in C# you have to have a relatively good understanding of programming methodology(am I wrong?).

                                True. VB family supports unstructured error handling like On Error Resume Next but such constructs are totally not supported in C#.

                                Vasudevan Deepak Kumar Personal Homepage
                                Tech Gossips
                                A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups