+ Reply to Thread
Results 1 to 5 of 5

For each loop very slow

  1. #1
    Registered User
    Join Date
    04-02-2012
    Location
    Brasil
    MS-Off Ver
    Excel 2010
    Posts
    20

    For each loop very slow

    Hello, I have the following macro


    Please Login or Register  to view this content.
    With small column ranges its working fine, but I need it to compare columns with 3000 or more rows. The first time I ran this with 2000 rows it took a while but worked but now if I run the macro the excel crashes.
    Any ideas on how to improve the macro?

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: For each loop very slow

    CassioGodinho,

    Welcome to the forum!
    What is the macro trying to do? You're updating the same cell over and over and over again until you finally go to the next cell in the A column... Seems like a lot of redundancy there, but without knowing the whole picture, it's hard to help out.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    04-02-2012
    Location
    Brasil
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: For each loop very slow

    Hi, thanks for the reply tigeravatar.
    Let me show some screenshots to explain.

    Pic1.PNG

    The macro is suposed to compare column A with column F, if the values match the value of column G is copied to the column B (using the .Next to copy the value to the cell in front of the match)

    Pic2.PNG

    The result is this. The macro works the way I want, but its very slow and "heavy", so if I compare large columns it takes forever and in most cases the excel crashes.

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: For each loop very slow

    CassioGodinho,

    Does it have to be a macro? What you're looking for can be accomplished with a formula in cell B3 and copied down:
    =IFERROR(VLOOKUP(A3,F:G,2,0),"")


    If it has to be a macro, you can input the formula programmatically and then convert the results to values, no loop necessary:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-02-2012
    Location
    Brasil
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: For each loop very slow

    Oh god, thanks a lot tigeravatar.
    I just started learning about vba macros that sometimes I want to make anything using a macro and forget about the power of excel formulas =)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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