Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Coding standards (all)

    (Friday afternoon quiz)
    Am I losing it, or is this:
    <pre> For rowinsert = 5 To 4 * y + Extrarows Step 2
    If Cells(rowinsert, 3).Value >= 0 Then
    Rows(rowinsert).Select
    Selection.Insert Shift:=xlDown
    ElseIf Cells(rowinsert, 3).Value = False Then
    GoTo 55
    End If
    55
    Next rowinsert</pre>

    equivalent to this:<pre> For rowinsert = 5 To 4 * y + Extrarows Step 2
    If Cells(rowinsert, 3).Value >= 0 Then
    Rows(rowinsert).Select
    Selection.Insert Shift:=xlDown
    End If
    Next rowinsert
    </pre>


  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Lounger
    Join Date
    Nov 2006
    Location
    Denver, Colorado, USA
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding standards (all)

    I believe the two code segments will provide the same results but the second one is better for a couple reasons. 1) it has fewer lines of code so it's easier to read and will save you some clock cycles and 2) You should avoid using a Goto.

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

    Re: Coding standards (all)

    Also, it's better to avoid selecting cells if possible: you can replace

    Rows(rowinsert).Select
    Selection.Insert Shift:=xlDown

    with

    Rows(rowinsert).Insert Shift:=xlDown

    or even

    Rows(rowinsert).Insert

    since if you insert an entire row, the shift is down by default.

  5. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding standards (all)

    > I believe the two code segments will provide the same results

    Thanks Patrick. That's the confirmation I was looking for.

    The orphaned code is riddled with obtuse GoTos like this one. I was beginning to think I'd lost my mind.
    I agree on spurning (shunning) GoTos.
    They will soon be all, well Gone!

  6. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding standards (all)

    > it's better to avoid selecting cells if possible:
    Thanks for this Hans.
    I agree, but I hadn't got as far as converting selections to range. That comes tomorrow!

    As you might guess, it's '000s of lines of recorded macros, modified, and then handed to me.

  7. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Coding standards (all)

    Also beware of code that steps forwards through rows whilst adding or removing rows, it might not do what you expect as each insert or delete will change the row numbers underneath you. It is generally safer to do this by starting at the bottom and using a negative step.

    StuartR

  8. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding standards (all)

    > code that steps forwards through rows
    Quite so!

    My first pass (in reduction) is reducing logical components; that is, removing code that can be shown to have absolutely no impact on the logic of the code.
    Hence my immediate interest in GoTos that merely branch out of an IF statement or a FOR statement.

Posting Permissions

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