+ Reply to Thread
Results 1 to 10 of 10

How to make my code faster?

  1. #1
    Registered User
    Join Date
    03-24-2014
    Location
    Vienna, Austria
    MS-Off Ver
    Excel 2010
    Posts
    72

    How to make my code faster?

    Hey guys I have a working code, which I would like to make faster, but I'm struggling a bit.

    A few things:
    1) It's relatively slow, takes around 10 seconds (for Number = 1... + 2secs for each additional Number), for relatively easy tasks. (Match formula and depending on result -> replace corresponding values in other cells)
    Is there any other method to do what i want to do?
    2) With my given current code: I don't really know how to make a loop with my code. I know that first thing I can do is: Dim WkRow(1 to 19) as long. Then i know how I would change the WkRow formula... would do like that:
    WkRow(i) = Evaluate("MATCH(Calc2!J" & i + 1 & " & Calc2!L" & i + 1 & ",Stock_List!$A:$A&Stock_List!$C:$C,0)")
    b.Cells(WkRow(i), "D") = 1
    b.Cells(WkRow(i), "F") = 4


    ... But how would I include the "If Number" statement in a loop?

    3) Would a CalculationManual help somewhere to speed everything up? If yes, where?



    That's the code:

    http://pastebin.com/dATfiGSi

    Thanks a lot! I'm glad for any help/suggestion.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to make my code faster?

    Hello chrisignm,

    When working with Ranges, you can increase execution speed by limiting your Range size to only cells that have information. Declaring a Range as a full row or column will take much longer to process than just the cells that have data,
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: How to make my code faster?

    Chris,

    Yeah Leith is correct. It may be helpful to loop over cells in the b.UsedRange, where b is defined as the worksheet in your code. But I've never seen such a huge If-ElseIf statement.

    It also looks like you're trying to implement worksheet functions in VBA. I think you can speed up your code by doing whatever your worksheet function does in VBA code instead of the worksheet function with the Evaluate() funciton.

    What are you trying to do? What does "Number" and "WkRow1"..."WkRow19" represent?

    In order to loop over cells in a range, the structure looks like:

    For Each C In b.UsedRange
    '/Do something here
    '/C.Value returns the value in the cell
    Next C

    Joe
    Last edited by joe31623; 01-12-2016 at 07:14 PM.
    <---If my answer helped, please click *

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: How to make my code faster?

    Not sure about speeding the code up, though Leith does have a good point regarding ranges, but couldn't you cut the code down to this?
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    03-24-2014
    Location
    Vienna, Austria
    MS-Off Ver
    Excel 2010
    Posts
    72

    Re: How to make my code faster?

    Hey everybody! Thanks and I will try your suggestions (& try to explain @Joe) everything tomorrow!

    @Norie your loop looks really promising!! Thank you. I think in order to meet my requirements I would just have to change to:
    For I = Number To 1 Step -1

    => I don't need the +1 and also I want to it to go down until 1

    Thanks so much everyone!

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: How to make my code faster?

    In the code in the link you were going from Number + 1 down to 2.

    For example if Number was 2 the code went from 3 to 2, if Number was 10 the code went from 11 to 2 and so on.

  7. #7
    Registered User
    Join Date
    03-24-2014
    Location
    Vienna, Austria
    MS-Off Ver
    Excel 2010
    Posts
    72

    Re: How to make my code faster?

    Ahhhhhh sorry, I just understood. You are right *gg* Thanks!! Already love your code without even trying it :D

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: How to make my code faster?

    I'm confused, for Number = 1 & 2 this was the code.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    03-24-2014
    Location
    Vienna, Austria
    MS-Off Ver
    Excel 2010
    Posts
    72

    Re: How to make my code faster?

    Yep, I edited my post
    Sorry for confusion

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: How to make my code faster?

    No problem.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Make this code more efficient and faster
    By SIMBAtheCAT in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2014, 10:45 AM
  2. [SOLVED] Make code Faster
    By zplugger in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-30-2013, 10:08 AM
  3. Make VBA code faster
    By Danielle22 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-28-2013, 09:01 AM
  4. [SOLVED] Ways to make my code run faster
    By JazzyBear in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-03-2013, 03:59 PM
  5. [SOLVED] Make code run faster
    By ozhunter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2013, 04:26 AM
  6. Is it possible to get rid of this loop to make the code faster?
    By mhw1129 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-09-2011, 09:03 AM
  7. Make code faster/better best practices
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 12-29-2008, 10:25 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1