+ Reply to Thread
Results 1 to 8 of 8

Help with code efficiency - routine takes WAY too long to run

  1. #1
    Registered User
    Join Date
    09-24-2014
    Location
    NC
    MS-Off Ver
    2010
    Posts
    10

    Help with code efficiency - routine takes WAY too long to run

    I'm a VBA novice. I have a spreadsheet with 5 columns of data in columns A through E with any number of rows (usually thousands). The problem is that for some of the rows the data is shifted to the right one column (in columns B through F). I wrote a macro to shift the rows that were off; the macro evaluates each row and if column A is empty, it cuts the 5 cells to the right and pastes them and then moves to the next row, if not empty it just moves to next row. What I came up with works, but takes way too long and heats up my laptop. I've searched ways to make the code more efficient, but have not come up with something that does the trick.

    I'm requesting two things. First, given the code I have (the process of checking each row one by one), is there something I can do to make it run faster? Even if there is a better way to approach this, I'd love to learn how to make something like this faster if that is possible.

    Second, I know there has to be a better way to approach this. Any suggestions will help me learn how to do things better.

    Thanks in advance.
    Doug

    Please Login or Register  to view this content.
    Last edited by robertsfd2002; 06-24-2018 at 12:33 PM.

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

    Re: Help with code efficiency - routine takes WAY too long to run

    do you care about the format? if not maybe

    Please Login or Register  to view this content.
    If you want something done right... find a forum and ask an online expert.

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

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Help with code efficiency - routine takes WAY too long to run

    Try this on a copy of your workbook first.
    Change Sheet reference as required.
    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor Maudibe's Avatar
    Join Date
    12-21-2016
    Location
    USA
    MS-Off Ver
    2010-2013
    Posts
    326

    Re: Help with code efficiency - routine takes WAY too long to run

    Robert,

    When writing code, always attempt to avoid using the .Select method as it drastically slows down your code especially when you are working with thousands of rows. Scottiex shows this nicely

    Maud

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

    Re: Help with code efficiency - routine takes WAY too long to run

    @ robertsfd2002

    Please use Code Tags when posting code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    Also you might consider using For Each ... Next loop instead of For x = y... Next which is slower then For Each.
    Please Login or Register  to view this content.
    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.

  6. #6
    Registered User
    Join Date
    09-24-2014
    Location
    NC
    MS-Off Ver
    2010
    Posts
    10

    Re: Help with code efficiency - routine takes WAY too long to run

    All these are great and work; thanks for the help. scottiex's did require an additional line to clear out the column F remnants. All three ran instantaneously and give me three ways to study to learn.

    Thank you again for all the help. I am sure I'll be back with another problem and eventually I hope I can help others.

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

    Re: Help with code efficiency - routine takes WAY too long to run

    woops... I should have included one more cell in the range A:F and B:G (or tidied up as I assume you did).
    thanks for the feedback.

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

    Re: Help with code efficiency - routine takes WAY too long to run

    Glad to help and thanks for rep+.

+ 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] This code takes way to long to run
    By Jym396 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-31-2017, 11:07 AM
  2. Inefficient code - macro takes too long
    By dantray02 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 03-07-2014, 09:29 AM
  3. [SOLVED] Long VBA Code - Improve Efficiency?
    By Dgp2012 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-18-2013, 11:29 AM
  4. efficiency recomendations, code taking long time to execute...
    By am_hawk in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-04-2013, 03:55 PM
  5. [SOLVED] To improve Efficiency of code, code running too long
    By andywsw in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-19-2012, 05:54 PM
  6. Long code takes an age to run
    By E3iron in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-05-2009, 07:38 AM
  7. My Code takes too long to execute
    By coreytroy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-30-2008, 12:12 AM

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