+ Reply to Thread
Results 1 to 10 of 10

Speed up code

  1. #1
    Registered User
    Join Date
    02-23-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    10

    Speed up code

    Hi all,

    Would someoe be so kind and suggest a way I could speed up this code. The code is basically looking for specific data and coping the row onto a new sheet but it is taking several minutes to run and often comes up ith a 400 error.

    Thanking you kindly in advance.

    Taffy
    Attached Files Attached Files

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Speed up code

    Almost impossible to say without seeing the code in the context of its workbook.

    Please upload, and if there are zillions of rows remove most and just leave a reperesentative sample.

    If you are using any loops to extract data rows one by one then almost certainly the answer will be to use a Data Filter to extract all thh relevant rows with one instruction.

    I did note that you have a lot of Select instructions peppered throughout what code I did look at. Selecting stuff is rarely necessary. Just address the objects directly.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    02-23-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    10

    Re: Speed up code

    Thanks for the quick reply Richard. I've attached the wb.

    I've limited to 2000 rows but I'm looking at data upto 10000.

    Thanks
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Speed up code

    You could easily use a filer code which is much faster than a loop. You could have also shortened all the 5 codes you have in a single code loop. Right now, I have not got the time to re-work the code, but if no one has replied by the end of the day, I will do it.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Speed up code

    Hi,

    See the attached.

    The macro is in Module 1 and is
    Please Login or Register  to view this content.
    I added the dynamic range name "Data" which is used by the procedure and also the criteria range at the top of the output sheet.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    02-23-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    10

    Re: Speed up code

    UNBELIEVABLE it works a treat. It's gone down from 2/3 mins to run to 2/3 seconds. Thank you very much.
    One thing the missing criteria that the macro is spitting out ("-","unknown","other", "other,other") can these be highlighted in the output as these are the fields that are outstanding.

    Thanks in advance

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Speed up code

    Quote Originally Posted by relo View Post
    UNBELIEVABLE it works a treat. It's gone down from 2/3 mins to run to 2/3 seconds. Thank you very much.
    One thing the missing criteria that the macro is spitting out ("-","unknown","other", "other,other") can these be highlighted in the output as these are the fields that are outstanding.

    Thanks in advance
    Data filtering is the fastest way I know of grabbing data that meets certain criteria. I'm constantly surprised at the number of people who go for the inelegant 'brute force' looping code method. Every pass through the loop means that the Macro has to jump back to the Excel App and then return to the VBE and each time it does this there is a time overhead.

    I'm not sure what you mean by highlighting "-","unknown","other", "other,other". I understand the "-" because this is one of the criteria, but what are the others?Can you manually highlight the output you are referring to so that I can understand what's needed.

  8. #8
    Registered User
    Join Date
    02-23-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    10

    Re: Speed up code

    Hi Richard,

    My first thought was to loop, I always though data filtering was too simplistic for VBA. Such a novice. As for the output, my original code included:

    If c.Value = "-" Or c.Value = "" Then
    and

    If c.Value = "-" Or c.Value = "unknown" Or c.Value = "other" Or c.Value = "other,other" Or c.Value = "" Then

    and the code would also highlight the colour once locating the text string

    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlTextString, String:="unknown", _
    TextOperator:=xlContains
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0


    With that in mind I would expect the output to look some like the attached. It would seem that we have only been looking at "-" instead of searching for "-" and "", "other", "other,other", "unknown".

    Thanks in advance
    Attached Files Attached Files

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Speed up code

    Hi,

    I can only see 'other' in column V. There don't appear to be any "unknown" or "other,other"

    However as a general principle if there are more OR criteria which these are, they need a new criteria. So for instance to pick up the Column V 'Property Type' 'other' records you'll need to add a new criteria. At the moment there are two criteria for property type in E6 & F7. Hence you need to introduce a new row 8 and a new column G. In G1 enter "Property Type and in G8 enter 'other'. The whole criteria range should now be A1:O16.

    Don't forget to correct the output row of field names if you've used Insert to insert the new column G.

  10. #10
    Registered User
    Join Date
    02-23-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    10

    Re: Speed up code

    Brilliant thank you very much. Much appreciated.

+ 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] VBA Delete Row Code - Need more efficient code to speed it up
    By matcapir in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2015, 04:32 PM
  2. [SOLVED] Speed up the code
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-23-2015, 12:03 PM
  3. [SOLVED] Help to speed up code
    By saintsphan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-01-2015, 12:14 AM
  4. How to speed up this code
    By Surreall in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-12-2013, 10:26 AM
  5. Speed up code
    By dalewms2 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-14-2011, 01:29 PM
  6. [SOLVED] Ned to speed up my code
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-14-2006, 01:15 PM
  7. Speed up code
    By Derick Hughes in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-08-2005, 02:06 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