+ Reply to Thread
Results 1 to 29 of 29

Speeding up a code

  1. #1
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Speeding up a code

    Good morning Happy campers,

    I have the following code in a sheet.
    Please Login or Register  to view this content.
    This code works fine, however it takes around 20 seconds to complete. after a bit of old google searching i managed to find the following sections of code that may help.
    Please Login or Register  to view this content.
    but when i add this to my code it only does part of if, and seems to stop.

    I also found that i need to enter the following at the end of my code
    Please Login or Register  to view this content.
    Could anyone let me know where these lines should go in, in order for it to work?

    regards, galvinpaddy

  2. #2
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Apologies, i have posted the wrong code strings (duh)

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Speeding up a code

    Hello galvinpaddy,

    Here is an example you should be able to incorporta into your code.

    Please Login or Register  to view this content.
    Another thing that slows down code is selecting

    This:
    Please Login or Register  to view this content.
    Can be turned into:
    Please Login or Register  to view this content.
    Beyond Excel’s Macro Recorder
    Last edited by jeffreybrown; 06-24-2012 at 08:20 AM.
    HTH
    Regards, Jeff

  4. #4
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Hi Jeff, thanks for that, now, when running the code brings up a run-time error 1004 and highlights the following line -
    Please Login or Register  to view this content.
    Am i correct in thinking the data that was originally copied has now for some reason not been copied?

  5. #5
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Speeding up a code

    It would appear so.

    What are you trying to accomplish?

  6. #6
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    In short fella, the first part of the first code, C8 was copied, i then want this pasted into a cell. (the code finds the correct place to add it)

    Cheers

  7. #7
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Changed it slightly to read -
    Please Login or Register  to view this content.
    This seems to run a few seconds faster, but then does not return to the original sheet and clear the contents.

  8. #8
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Ok, so far i have managed to get the code to run VERY fast, but now cant get it to return to the starting sheet.

    Please Login or Register  to view this content.
    Once the code does its thing, it wont switch back to the 'Input' sheet.

  9. #9
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Speeding up a code

    Did you try to record your actions using the macro recorder to get the proper syntax?

    From PM:
    I think this is correct

    https://www.box.com/s/1d5de87955a7f16f6a19

    It works to a certain degree now, just needs to be able to switch back to the first tab after completing its code.

    Very much appreciated!!

  10. #10
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Hi Jeff,

    Initially yes, but as the code progressed from a very simple copy, select sheet, paste. it was then a case of copy a line of code, insert it.
    Having just had another look at it, i changed the last section of code to read
    Please Login or Register  to view this content.
    Now, the code runs as i want, it does take about 5-6 seconds to complete, which is a great improvement on before, but still want to know how to reduce the wait time to as little as possible (if even possible)

    Kind Regards.

  11. #11
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Speeding up a code

    I know nothing about your code, but ...

    Please Login or Register  to view this content.
    Normally, when you use a with statement, you use range and/or cells with a dot before them. For example:

    Please Login or Register  to view this content.
    Point 1: In this case all this means is:
    Please Login or Register  to view this content.
    The dot (without anything else before it) connects the Range with the with statement.
    So there is not much call for using a With ... End With statement for just one item.

    Point 2: Without the dot, then range refers to the active sheet.

    If you are trying to speed up your code, normally you shouldn't be using activecell, select, of selection.

  12. #12
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Speeding up a code

    Please Login or Register  to view this content.
    Here is another example, just use:

    Please Login or Register  to view this content.
    In fact, since it is only one cell, why do you need to copy it at all?

    Is there any chance you can upload your workbook?

  13. #13
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Hi StevenM,
    The reason for copying Cell C8 is i need to then paste it in another section, on Post9 above there is a link to Box.Com for the file, its almost 2mb so cant load it here.

    Thanks for your input so far.

  14. #14
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Speeding up a code

    Ok, so far i have managed to get the code to run VERY fast, but now cant get it to return to the starting sheet.
    Answer: Don't active any other sheet. :-) Seriously, why change to another sheet?

  15. #15
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Hi StevenM,

    Thats because the sheet will be used by members of my team, and aid damage limitation, i need to make it as easy as possible for them to complete :D

  16. #16
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Speeding up a code

    My point is this, if you have only one cell to copy, and you want to copy its value, why not:

    Worksheets("Whatever").Range("Cell Address").Value = Worksheets("Where Ever").Range("Cell address").Value

  17. #17
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Ah i see, thanks
    Forgive me if i miss or dont get the point straight away, im still learning the basics for VBA :D

  18. #18
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Speeding up a code

    Please Login or Register  to view this content.
    Better would be:

    Please Login or Register  to view this content.
    VBA keeps the length of each string embedded at the beginning of each string. So finding the length of a string is easy for VBA and is faster than comparing it to a zero length string. Also, 0 = False, anything else = True. Thus:

    Please Login or Register  to view this content.
    Is better than If Len(FindString) > 0 Then[/CODE]

    Now, about the following:

    Please Login or Register  to view this content.
    Here you should just have:
    Please Login or Register  to view this content.
    Now, instead of:

    Please Login or Register  to view this content.
    You should have:

    Please Login or Register  to view this content.
    Assuming that this range's worksheet is active, if not then:

    Please Login or Register  to view this content.
    Then:

    Please Login or Register  to view this content.
    There is no need to goto the location of Rng in order to use the location of Rng.

  19. #19
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Hi StevenM,First off, thanks alot for the detail in the above, all help greatly appreciated!
    Code has been changed to
    Please Login or Register  to view this content.
    But still takes 6-7 seconds to run, if possible could i trouble you for more help on it?

    kind regards. galvinpaddy

  20. #20
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Sry fella, one more thing, i now no longer get the date added with
    Please Login or Register  to view this content.

  21. #21
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Speeding up a code

    I downloaded your workbook. If I knew what values to put in C4 & C8 to test it, I could.

    I combined your two macros into one.

    Assuming I didn't make some mistake, the following should work. Anyway, let me know if it does.

    Please Login or Register  to view this content.

  22. #22
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Speeding up a code

    I guess you can also try:

    Please Login or Register  to view this content.
    instead of

    Please Login or Register  to view this content.

  23. #23
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Hi StevenM,
    Thanks for that, will test shortly!
    As for the info to input In C4 you can put any one 5 digit codes in ColumnA (provision tab) and then in C8 can be anything.
    If for example you input the code - 13563 then a random 6 digit code in C8, click the button, then find that 6 digit code on the second tab, and you see the changes there.

  24. #24
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Good morning all.

    Zbor - Thanks for your input
    StevenM - Thanks for your also!
    I have ran the sheet with your code and i am still finding that Excel takes 7 seconds to run the cycle. Above should explain how to test the sheet & its code, if not let me know

    Many thanks all.
    Regards, galvinpaddy.

    **EDIT**
    I also no longer see the date being eneterd into the cell as required on sheet ("Work Orders")
    Last edited by galvinpaddy; 06-25-2012 at 01:36 AM. Reason: Added addtional issue

  25. #25
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Speeding up a code

    Are you wanting to copy the date from Work Orders or add today's date?

    Because the line:

    Please Login or Register  to view this content.
    Adds Today's date to column "B" of the line found.

  26. #26
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Speeding up a code

    The line which is slowing down your macro is:

    Please Login or Register  to view this content.
    So, how about just calculating the one row instead of the whole sheet? (I also made a couple other small modifications.)

    Please Login or Register  to view this content.

  27. #27
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Quote Originally Posted by StevenM View Post
    Are you wanting to copy the date from Work Orders or add today's date?

    Because the line:

    Please Login or Register  to view this content.
    Adds Today's date to column "B" of the line found.
    Hi, Ideally i need to capture the days date as the code was ran (the same as =TODAY() sort of thing)

    Will check the new code shortly, thanks again

  28. #28
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Speeding up a code

    Please Login or Register  to view this content.
    Last edited by snb; 06-25-2012 at 07:10 AM.



  29. #29
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Speeding up a code

    Hi All,
    Firstly - StevenM - the date part of your code was working, however i needed it to read -
    Please Login or Register  to view this content.
    it was only by searching for todays date in the ("Work Orders") tab that i noticed it, and then it suddenly made sense!!
    The code works, very well infact - runs in what seems like less than a second.

    SNB - I have also tried yours and it appears to do exactly the same.

    Many thanks to both, greatly appreciated.!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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