+ Reply to Thread
Results 1 to 11 of 11

VBA Code to Add Rows is slowing down

  1. #1
    Registered User
    Join Date
    01-19-2008
    MS-Off Ver
    Excel for M365 MSO
    Posts
    44

    VBA Code to Add Rows is slowing down

    Hello,

    For some reason, all of a sudden, the following code is slowing down when I see the additional rows being added one by one, whereas in the past, I would have the circle spin for a few seconds, and all added rows would appear. I could use some expertise in optimizing the code as I took bits and pieces from different sources as I'm not great at VBA. TIA

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

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,227

    Re: VBA Code to Add Rows is slowing down

    For starters. I suggest managing the screen updating outside the loop.
    Please Login or Register  to view this content.
    Since I can't run this in a blank file I also request that you attach the Excel file with some sample data so we do a dynamic analysis of the code.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    01-19-2008
    MS-Off Ver
    Excel for M365 MSO
    Posts
    44

    Re: VBA Code to Add Rows is slowing down

    Hi I know it's been a little while, but here is an example of the worksheet. As you'll see, when you click on the Add Row button, it goes through each cell to enter the information in each row. In my actual workbook, I have over 1000 rows and now it's starting to get very slow. Trying the best way to add "x" rows after the next available rows.

    Example.xlsm

    Thx for your help!

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

    Re: VBA Code to Add Rows is slowing down

    Have not looked at your attachment but another change needed is the selecting.
    Selecting slows down macros tremendously.
    Use With ....... End With instead or just Range(whatever range here).Formula = "= Blah Blah"
    The inherent weakness of the liberal society: a too rosy view of humanity.

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

    Re: VBA Code to Add Rows is slowing down

    In the "AddEntry" macro, you have "Dim i" which makes it a Variant. Should be "Dim i As Long" or "Dim i As Integer"
    See here:
    https://learn.microsoft.com/en-us/pr...ectedfrom=MSDN


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Just a couple things I noticed when glancing at your code.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,227

    Re: VBA Code to Add Rows is slowing down

    Your button calls a macro in another file

    'C:\Users\jbala\Desktop\TEST\Balaban_Finance.xlsm'!AddEntry

    which of course we don't have.

    I'll just assume it's OK to run the code directly by calling the sub instead of from a worksheet button

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,227

    Re: VBA Code to Add Rows is slowing down

    rowData does everything by selecting a range then acting on Selection. This is relatively slow, and is probably a result of using the macro recorder. The code should be changed to directly operate on the relevant ranges, but it will take a while to do the analysis and make the changes.

    What would really help is an English description of what you want to your code to do, so I don't have to spend as much time reverse engineering it.

  8. #8
    Registered User
    Join Date
    01-19-2008
    MS-Off Ver
    Excel for M365 MSO
    Posts
    44

    Re: VBA Code to Add Rows is slowing down

    Hi,

    I want to add rows to my sheet that include the formatting and formulas that I have in Module 2. They should be added after the last row that has data in it (determined by Column A).

    Thanks again!

  9. #9
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,545

    Re: VBA Code to Add Rows is slowing down

    I think the main reason for the slowdown is the large number of CheckBox controls. Many times I noticed slowing down with a large number of graphic objects in the sheet (controls are also graphic objects).

    Artik

  10. #10
    Registered User
    Join Date
    01-19-2008
    MS-Off Ver
    Excel for M365 MSO
    Posts
    44

    Re: VBA Code to Add Rows is slowing down

    What would you recommend instead of the checkbox?

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,227

    Re: VBA Code to Add Rows is slowing down

    You can use Worksheet_SelectionChange and put X's directly into the cell (or remove) when selected. See updated file. Also needed change to column J.

    To amplify earlier comments, you are using Select and then acting on Selection or ActiveCell. This consumes a lot of resources. It is better to directly address the cell than to Select it and then act on ActiveCell.
    Attached Files Attached Files

+ 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] 3 lines of code slowing workbook
    By Tpleme in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-09-2018, 06:10 PM
  2. VBA Code slowing down active sheet
    By pchugh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-23-2018, 01:53 AM
  3. VBA code is slowing down the worksheet function
    By bala04msw in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-07-2016, 09:09 AM
  4. vba code slowing things down.
    By Zimmerman in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-05-2015, 11:19 AM
  5. [Question] =IFCountA is slowing down my Code
    By SirRubberDucky in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2014, 12:35 PM
  6. Why this code is slowing down
    By TheMaciej in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-31-2013, 01:11 AM
  7. VBA code slowing up worksheet
    By dsrt16 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-25-2009, 05:40 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