+ Reply to Thread
Results 1 to 8 of 8

How to make the macro run faster?

  1. #1
    Registered User
    Join Date
    09-13-2012
    Location
    Waterloo, Canada
    MS-Off Ver
    Excel 2010
    Posts
    11

    How to make the macro run faster?

    So I have the below code, it works but very slowly. What it does it that for each brokercode in the active workbook (let's called it #1), it searches the mainFile workbook for that value. Once found, it will apply the same color to the brokercode cell. Would anyone have a better idea to do this? I have 5000 entries and the macro sometimes freezes before giving the results. I'm not sure if it is because each brokercode in workbook #1 has duplicates, so there are many iterations doing the exact same thing over and over. Can anyone please help to make the macor run faster? I don't know how to do!


    Please Login or Register  to view this content.
    Last edited by jeffreybrown; 09-13-2012 at 11:35 AM. Reason: Added code tags for new users...please add them next time when posting code.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to make the macro run faster?

    Hi, ahng,

    not switch between the files and instead of using a loop use either Find or WorksheetFunction.Match (maybe after having assured that there is a value with WorksheetFunction.CountIf). Do you need some code for these assumptions or can you go on with these on your own?

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    09-13-2012
    Location
    Waterloo, Canada
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to make the macro run faster?

    Could you help me with the code too? I'm not sure how to not switching between files. The mainFile workbook is a master fille and could only be read-only. It it is a list of brokers across all provinces and its column D is color coded by the broker's performance

    Many thanks in advance,
    Ann

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to make the macro run faster?

    Hi, Ann,

    code is untested but should be faster than your loop:
    Please Login or Register  to view this content.
    Regarding
    The mainFile workbook is a master file and could only be read-only.
    Dark clouds ahead - check for read-only prior to executing the code by
    Please Login or Register  to view this content.
    If true throw up a message (or save under a different name depending on how you would like to proceed).

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    09-13-2012
    Location
    Waterloo, Canada
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to make the macro run faster?

    Hi Holger,

    Thanks a lot for your help. I just have time to check the code today. I kept getting error msg "Tyoe mismatch" for the line

    Please Login or Register  to view this content.
    I've checked and tried to modify a bit but still keep geting "Type mismatch" error. Could you please help? I'm sorry, I'm a very newbie for VBA

    Thanks,
    Ann

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to make the macro run faster?

    Hi, Ann,

    like I stated: code was untested (and with a lot of typos even in the variables).

    Please give this code a try - I tested this one on a workbook and it worked fine there (I also deleted the Exit For which would have stopped the macro after the first found an not work on the other cells in the range):

    Please Login or Register  to view this content.
    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    09-13-2012
    Location
    Waterloo, Canada
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to make the macro run faster?

    I tested and it works perfectly. You're right, the "Exit For" was the problem. Last time, I fixed the typo in variables but didn't see the problem with the for loop.

    Thanks a lot for your help!
    Ann

  8. #8
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: How to make the macro run faster?

    @ ahng

    Welcome to the forum.

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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