+ Reply to Thread
Results 1 to 14 of 14

If / Then Code Running Very Slow

  1. #1
    Registered User
    Join Date
    09-01-2019
    Location
    Australia
    MS-Off Ver
    Office 365
    Posts
    7

    If / Then Code Running Very Slow

    Hi,

    I'm working on VBA code to achieve the following:

    - In Worksheet "One", search a column for values that are greater than specified
    - If the criteria is met, then copy columns 2 and 4 to destination specified in Worksheet "MASTER"

    The issue I am having is that the code takes a very long time to execute because there are so many rows to check in Worksheet "One" (over 12K)

    Is there a way for me to make the code below more efficient?

    Any pointers would be greatly appreciated



    Please Login or Register  to view this content.
    Last edited by tswood; 09-02-2019 at 01:54 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: If / Then Code Running Very Slow

    Hello TSWood,

    As far as I can tell, you are searching Column P of the "One" sheet for any value greater than that specified in cell Y1 of the "Master" sheet and then transferring the corresponding values from Columns B and D of the "One" sheet to Columns A and F in the "Master" sheet.

    If I've understood this correctly, then the following may help to speed it up a little for you:-

    Please Login or Register  to view this content.
    I hope that this helps.

    Cheerio,
    vcoolio.
    Last edited by vcoolio; 09-02-2019 at 01:48 AM. Reason: Typos

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Hi !

    Quote Originally Posted by tswood View Post
    Is there a way for me to make the code below more efficient?
    An easy & faster way : an advanced filter needing a single codeline to filter & copy …

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: If / Then Code Running Very Slow

    Welcome to the forum! Good job posting code and explaining what you need.

    Advanced filter copies only to the filter range's sheet. I guess one could manually move the two columns after the filter or make code setup the criterion range and conditions, copy/paste, and delete copied to columns. I showed the manual way in the attachment. See AA1 and right in sheet One.

    I would use autofilter somewhat similar to vcoolio's. I just tweaked his code a bit. Always test code on a backup copy. I also attached my simple file if you want to play with it.

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

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    As the initial post asked for copy and not for move, the reason why I talked about an advanced filter …
    It just needs the same headers spelling in the destination worksheet and a criteria range.

    If only an attachment with a before context and an expected result …

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: If / Then Code Running Very Slow

    Modifying the OP's original code a bit I think this should work.

    Please Login or Register  to view this content.
    Alf

  7. #7
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: If / Then Code Running Very Slow

    Hi Kenneth,

    In your tweaked code:
    x As Integer
    Thanks for the prompt! I should probably get back into the habit of heading the code with "Option Explicit".

    @Marc L:-

    I agree with Kenneth.

    As the initial post asked for copy and not for move
    If you had carefully read the OP's opening post, including the code supplied, you would have realised that he is wanting to copy and paste. Is this not transferring (or moving) data?

    Cheerio to all.
    vcoolio.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    As 'moving' sounds like cut / paste instead of copy / paste like under the files explorer : do you move or just copy a file ? …

    If only an attachment with the expected result but for general purpose like filtering & copy the advanced filter may well do the job.

  9. #9
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: If / Then Code Running Very Slow

    Quote Originally Posted by Kenneth Hobson View Post
    Advanced filter copies only to the filter range's sheet.
    FYI, that isn't correct. The source range, criteria range and target ranges can all be on any sheet you like. They don't even have to be in the same workbook.
    Rory

  10. #10
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: If / Then Code Running Very Slow

    Actually, the rule is that the CopyTo range MUST be in the activesheet. Obviously, the other two inputs can be on any sheet.

    Here is how to do it with AdvancedFilter named sheet as activesheet. If one were to run the code from another sheet, it would fail unless the copyto sheet was activated by the code before the filter. With the button on the sheet, it is already activated.
    Please Login or Register  to view this content.
    I modified the file to show both methods.
    Attached Files Attached Files

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: If / Then Code Running Very Slow

    Quote Originally Posted by Kenneth Hobson View Post
    Actually, the rule is that the CopyTo range MUST be in the activesheet.
    What do you base that on? There is no such requirement that I know of.

  12. #12
    Registered User
    Join Date
    09-01-2019
    Location
    Australia
    MS-Off Ver
    Office 365
    Posts
    7

    Re: If / Then Code Running Very Slow

    Hi All,

    Thank you for your help with the code, I very much appreciate it. Just to clarify, I want to 'copy' not 'move' the data.

    I've tried a variation of the code provided (adding a few additional columns to copy and paste) but am not having much luck.

    Currently, the code clears all data from ws1/ Sheets("One") from row 2 down. This includes columns not referred to in the code.
    Secondly, the data is not appearing in ws/ Sheets("Master"). This remains blank.

    Here is the code I am using. Have I perhaps typed something incorrectly or am I missing a crucial part?

    Please Login or Register  to view this content.

  13. #13
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: If / Then Code Running Very Slow

    I can't see anything there that could clear data. Are you sure it's not just hidden by a filter? Posting a workbook would be very helpful.

  14. #14
    Registered User
    Join Date
    09-01-2019
    Location
    Australia
    MS-Off Ver
    Office 365
    Posts
    7

    Re: If / Then Code Running Very Slow

    Hi rorya - you are right, yes. The data is hidden not cleared. Is there a way for me to stop this?

    The data still isn't coping to the new sheet though.

    Unfortunately I can't post the workbook I'm using but can try creating something similar.

+ 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. Slow running Code
    By Ausadian in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-25-2015, 04:41 PM
  2. Slow running code
    By phil2006 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-13-2013, 08:02 AM
  3. vba code running too slow
    By hitsujicute in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-31-2013, 06:00 PM
  4. VBA Code running very slow. Need help
    By krjoshi in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-21-2013, 02:13 PM
  5. Macro code is running to slow
    By chsaav in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-23-2011, 05:00 AM
  6. Slow running code
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-06-2008, 12:49 PM
  7. Code running slow
    By lou031205 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-08-2007, 12:20 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