+ Reply to Thread
Results 1 to 14 of 14

Help speeding/cleaning my lengthy macro

  1. #1
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Help speeding/cleaning my lengthy macro

    I am very new to the InStr and this was what I was able to create, but it is slow. Is there something else I should have used? How do I speed it up?



    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Help speeding/cleaning my lengthy macro

    Part two

    Please Login or Register  to view this content.

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Help speeding/cleaning my lengthy macro

    I wonder if it would speed up if you made the items you are searching an array and place the array in the Instr function. It would definitely shorten the codes(s).
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  4. #4
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Help speeding/cleaning my lengthy macro

    I wondered about that, but since each search goes has a different destination, i didn't know where to begin.

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Help speeding/cleaning my lengthy macro

    Use the If-Then-Else aspect of VBA. Create a different Range to search for each array and location to place results. It won't be short code and will probably still take a while as you are running several searches. Add
    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    Turn off any auto calc at the beginning and reactivate at the end of your code. These will speed things up a bit.

  6. #6
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,347

    Re: Help speeding/cleaning my lengthy macro

    1) do all the work inside an array
    2) find the lastrow so you don't accidentally get 1 million rows

    Please Login or Register  to view this content.
    (obviously "set rng" like you had - if you stay with using the sheet range)
    Last edited by scottiex; 10-26-2017 at 06:21 PM.
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  7. #7
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Help speeding/cleaning my lengthy macro

    OK, if then Else is new to me, so I'll explore that, but can I put the False at the start at the beginning of my very first macro and then at the end of the very last, or do I need to do it in each macro.

  8. #8
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Help speeding/cleaning my lengthy macro

    Quote Originally Posted by scottiex View Post
    1) do all the work inside an array
    2) find the lastrow so you don't accidentally get 1 million rows

    Please Login or Register  to view this content.
    Hey Scottie, where would that go?

  9. #9
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,347

    Re: Help speeding/cleaning my lengthy macro

    To get it all in an array we have quite a bit more work to do so keeping it simple just put this form

    Please Login or Register  to view this content.
    wherever you have this form

    Please Login or Register  to view this content.
    and see if that helps.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,338

    Re: Help speeding/cleaning my lengthy macro

    It seems to me that you are doing a lot of recursive stuff just for nothing.

    Do all the values you are looking up occur once or multiple times in the searchrange ?

    If they occur only once it's better to use something like Application.Match or Find to lookup your value and copy the corresponding value.

    If they occur multiple times it's better to lookup the last occurence because it's corresponding value will be the one that finally will be in your destination range.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Help speeding/cleaning my lengthy macro

    This is slow
    Please Login or Register  to view this content.
    This is fast, but moves only values not formatting.
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  12. #12
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Help speeding/cleaning my lengthy macro

    Bakerman2,

    i attached a workbook that shows what I start with and then what I end up with. The number of items listed under each person in the pivot table may change so an exact position isn't possible.
    Attached Files Attached Files

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Help speeding/cleaning my lengthy macro

    Given the loops in the code, at the very least it looks as though you should probably be using Exit For after finding the values you are looking for- otherwise you carry on looping through the ranges for nothing. I suspect also that you could simply use GETPIVOTDATA formulas and not loop at all.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  14. #14
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Help speeding/cleaning my lengthy macro

    Please Login or Register  to view this content.
    Looping cells in a range is the absolute slowest way to evaluate contents. Sometimes its the best way regardless, because its easy to implement and isnt prone to issues.

    You could speed things up by using range.find instead, and if need be in a loop range.findnext. Basically find returns a range object, you set that to a variable and then do a range.value = range.value to transfer its value to another cell. Ironically I see now you use this in the next sub.

    Depending on your data set you may also benefit from filter instead (either auto or advanced). You can filter on partial strings and then take the results and move them together (assuming you need more than 1 match for each term you seek).

    Also, For loops are good for when you need to do something x number of times regardless of the result in each iteration. If you need to loop until something is found or while something is true/false, instead of using exit for to leave the loop, just use a Do while or Do until loop. This will allow the loop to only iterate as many times as required.

    Changing references for ranges to be only the required range instead of full columns will help too, especially when using a for loop. In multiple places I see H:H instead of H2:H100 for example. If your looping every cell in H:H thats a million cells to check instead of 100, 1000, etc. Do this 20 times and its 20 millions cells to loop instead of 20,000. Also using constants can speed up code. Set a constant string for your range and then use the constant instead of quoted text strings manually entered multiple times. Use constants for text strings you repeat elsewhere in the code too.

    copy/paste is slow and sometimes buggy. Instead do range.value = range.value. You can combine that with copy formatting or just code to format the destination.

    Use variables for objects like sheets and workbook instead of repeatedly doing worksheets("")... do a worksheet object and assign it to a variable.

    Turning off screenupdating will improve speed and so will turning off automatic calculation until your macros complete.

    Overall, my guess is the biggest issue is the combination of whole column references combined with for loops checking each cell in those columns. Fix that and your code should run much faster.

+ 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] Need Help Speeding up my Macro.
    By disepyon in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-07-2017, 03:22 PM
  2. [SOLVED] Speeding up Macro
    By booney440 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-09-2017, 10:41 PM
  3. [SOLVED] Speeding Up Macro
    By ScabbyDog in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-03-2015, 11:15 AM
  4. [SOLVED] improving functionality of lengthy macro
    By Zealotwraith in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-26-2013, 11:43 AM
  5. [SOLVED] Speeding up my macro
    By jsuarez199 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-03-2013, 10:52 AM
  6. [SOLVED] Help in speeding up my macro!
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-31-2013, 12:04 PM
  7. [SOLVED] Help on cleaning / speeding up code
    By Chris Salcedo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-16-2005, 09:05 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