Consulting

Results 1 to 12 of 12

Thread: Which Runs Quicker???

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Which Runs Quicker???

    I have some code to format a column the way I want and I use similar code multiple times. I was wondering which would run quicker???

    detail.Range("S1").FormulaR1C1 = "Unit Cost ($)"
        detail.Range("S1").WrapText = True
        With detail.Columns("S:S")
        .TextToColumns Destination:=Range("S1")
        .Style = "Comma"
        .AutoFit
        End With
    or


    Range("S1").FormulaR1C1 = "Unit Cost ($)"
        Range("S1").WrapText = True
        With Columns("S:S")
        .TextToColumns Destination:=Range("S1")
        .Style = "Comma"
        .AutoFit
        End With

    the only difference is I removed detail from the front of the lines. Detail is the variable name I set to that sheet. I read in one of the articles here that the more periods you have the slower it would run. But I also read that using variables, so excel doesn't have to figure out where is quicker. So which one is quicker?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I can't believe that either of those would be differentiable in terms of speed.

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I know if I use it once it wouldn't hurt speed but what if they are used 25 times?? would that matter then???

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    25 times milliseconds is still very little.

    Concentrate on the big problems, such as loops, selecting ranges, et al.

  5. #5
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I got rid of most of the selecting. Now, what do you mean concentrate on loops???

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Well loops are often the time consumers. For instance, looping thorugh 10,000 rows deleting rows where a certain condition is met is slow. It is much quicker to filter on that condition, and delete the visible rows.

    There is always a trade-off between easy to understand, maintainable code, and finely tuned, optimised code. There are no cut and dried answers, just depends upon the circumstances.

    Often, turning screenupdating off and setting calcualtion to manual at the start and restting at the end can save huge amounts of time, your dot qualifiers don't even figure in performance issues.

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    how do you set calculation to manual?? And what does it mean??

    I already filter and delete selected. Thank you for all your help.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Application.Calculation = xlCalculationManual
    and set to xlCalculationAutomatic when completed.

    What it does it to stop Excel from recalculating every time something changes. So, if you write to the worksheet say 1000 times, Excel will recalculate 1000 times. That could be expensive on a busy worksheet, so turning calculation to manual avoids all of that. Setting it back to AUtomatic at the end menas that just one recalculation is invoked.

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You can test the time it takes a procedure to run...Ken has a function in the kb:
    http://vbaexpress.com/kb/getarticle.php?kb_id=542
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Thank you Lucas,

    I am going to try that

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

    Why have you removed detail from the code?

    If you do that then the range is unqualified so VBA will format the range on whatever the active sheet is.

  12. #12
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by Djblois
    I have some code to format a column the way I want and I use similar code multiple times. I was wondering which would run quicker???

    detail.Range("S1").FormulaR1C1 = "Unit Cost ($)"
    detail.Range("S1").WrapText = True
    With detail.Columns("S:S")
    .TextToColumns Destination:=Range("S1")
    .Style = "Comma"
    .AutoFit
    End With
    or

    Range("S1").FormulaR1C1 = "Unit Cost ($)"
    Range("S1").WrapText = True
    With Columns("S:S")
    .TextToColumns Destination:=Range("S1")
    .Style = "Comma"
    .AutoFit
    End With

    the only difference is I removed detail from the front of the lines. Detail is the variable name I set to that sheet. I read in one of the articles here that the more periods you have the slower it would run. But I also read that using variables, so excel doesn't have to figure out where is quicker. So which one is quicker?
    A large number of property calls inside a loop is what takes time.

    The idea of using either a variable or a With statement is simply to reduce the number of property calls on the inside of a loop and either way produces the same end (the With statement simply eliminates the need for any intermediate variable). As EL XID said, any speed difference is not even worth considering...
    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.

Posting Permissions

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