+ Reply to Thread
Results 1 to 13 of 13

Inserting blank rows now running very slow

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-15-2009
    Location
    Herndon, VA
    MS-Off Ver
    Excel 2010
    Posts
    163

    Inserting blank rows now running very slow

    This block of code inserts a blank row at every change in column A. The loop begins at the bottom. This used to run extremely fast. Now it's slow. Any ideas why?



       Dim firstRow As Integer
        Dim currentRow As Integer
        
        firstRow = 2
      
        
        For currentRow = Range("A1048576").End(xlUp).Row To firstRow Step -1
            If Range("A" & currentRow) <> Range("A" & currentRow).Offset(-1, 0) Then
               Range("A" & currentRow).EntireRow.Insert
            End If
        
        Next
    There is other code in this procedure. For some reason it takes a long time. When I run this code in its own procedure it zips right thru.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Inserting blank rows now running very slow

    Hi there,

    See if setting the calculation to manual (and then resetting it back to its original setting) helps i.e.

    Option Explicit
    Sub Macro1()
    
        Dim lngMyRow As Long
        Dim xlnCalcMethod As XlCalculation
        
        With Application
            xlnCalcMethod = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
        
        For lngMyRow = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If Range("A" & lngMyRow) <> Range("A" & lngMyRow).Offset(-1, 0) Then
               Range("A" & lngMyRow).EntireRow.Insert
            End If
        Next lngMyRow
        
        With Application
            .Calculation = xlnCalcMethod
            .ScreenUpdating = True
        End With
    
    End Sub
    From Excel 2007 and on it's a good idea to not use an integer variable when referencing rows as its highest value is 32,767 as opposed to there being 1,048,576 rows in Excel 2007 and on.

    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Contributor
    Join Date
    12-15-2009
    Location
    Herndon, VA
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Inserting blank rows now running very slow

    Quote Originally Posted by Trebor76 View Post
    From Excel 2007 and on it's a good idea to not use an integer variable when referencing rows as its highest value is 32,767 as opposed to there being 1,048,576 rows in Excel 2007 and on.
    Robert
    Nice suggestion. I didn't even think about that. I hope I will keep that in mind.

    I tried your code and it still takes the same amount of time to execute. Any other ideas? Does memory need to be cleared out or something?

    Also, I noticed that in your code you turn off/then turn on screen updating. Is there an advantage to doing this?

  4. #4
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Inserting blank rows now running very slow

    Quote Originally Posted by Excel_vba View Post
    This block of code inserts a blank row at every change in column A.
    The way the question is posted might be an event code? If thats the case I would just add one more thing to the amazing code Trebor76 provided.

    Option Explicit
    Sub Macro1()
    
        Dim lngMyRow As Long
        Dim xlnCalcMethod As XlCalculation
        
        With Application
            xlnCalcMethod = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        For lngMyRow = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If Range("A" & lngMyRow) <> Range("A" & lngMyRow).Offset(-1, 0) Then
               Range("A" & lngMyRow).EntireRow.Insert
            End If
        Next lngMyRow
        
        With Application
            .Calculation = xlnCalcMethod
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    
    End Sub
    Thanks

  5. #5
    Forum Contributor
    Join Date
    12-15-2009
    Location
    Herndon, VA
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Inserting blank rows now running very slow

    Quote Originally Posted by fredlo2010 View Post
    The way the question is posted might be an event code? If thats the case I would just add one more thing to the amazing code Trebor76 provided.
    What do you mean by event code?

  6. #6
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Inserting blank rows now running very slow

    A code thats controlled by and event is placed under the Sheet module or Thisworkbook. They are triggered by changes in the worksheet. For example if the even is triggered every time the sheet is calculated the code will run.

    Did you tried my code? Can you provide us with a sample of the workbook ?

    The code Trebor provided turns off calculations (set them to manual , and then back to whatever state it was on) so the formulas do not have to refresh and use memory. It also turns off screen updating so it does not use as much memory repainting and showing updated material.

    At this point the code should be bulletproof lol can you attach a sample of your workbook. Maybe there is something we are missing.

    Thanks


    Thanks

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Inserting blank rows now running very slow

    Try this
    Sub test()
        Application.ScreenUpdating = False
        Columns(1).Insert
        With Range("b3", Range("b" & Rows.Count).End(xlUp)).Offset(, -1)
            .Formula = "=if(b2<>b3,if(a2=1,""a"",1),"""")"
            .Value = .Value
            On Error Resume Next
            .SpecialCells(2, 1).EntireRow.Insert
            .SpecialCells(2, 2).EntireRow.Insert
            On Error GoTo 0
        End With
        Columns(1).Delete
        Application.ScreenUpdating = True
    End Sub

  8. #8
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Inserting blank rows now running very slow

    the amazing code Trebor76 provided
    Thanks fredlo2010 - not much compared to some of the code provided on this forum (jindon is a perfect example) but nice of you to say so

    Cheers,

    Robert

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

    Re: Inserting blank rows now running very slow

    Just for the heck of it.
    Don't take this wrong please as I certainly can't compete writing code with you people.
    Filled 50,000 rows in blocks of 10, (every 10th row the value changes).
    Ran all three codes twice each with a timer and got the following.
    Trebor76 first code, resp. 16.05 and 14.17 seconds
    Trebor76 second code (the code from fredlo2010's post) resp 11.61 and 12.36 seconds
    jindon's code resp 19.48 and 20.56 seconds.
    This is on a +/- 7 year old machine with 2 GB Ram and a 2.4 GHz CPU.

  10. #10
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Inserting blank rows now running very slow

    @jolivanes,

    as being already set up with test data and timing, perhaps you'd like to consider this one
    Sub insert_blank_rowsx()
    
    Dim r As Long, c As Long, a, u(), i As Long, k as long
    r = Cells.Find("*", , , , xlByRows, xlPrevious).Row
    c = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    ReDim u(1 To 2 * r, 1 To 1)
    a = Cells(1).Resize(r + 1)
    For i = 1 To r
        u(i, 1) = i
        If a(i, 1) <> a(i + 1, 1) Then k = k + 1: u(r + k, 1) = i
    Next i
    Cells(c + 1).Resize(r + k) = u
    Cells(1).Resize(2 * r, c + 1).Sort Cells(c + 1), Header:=xlNo
    Cells(c + 1).Resize(r + k).ClearContents
    
    End Sub
    Last edited by kalak; 12-23-2013 at 05:59 AM.

  11. #11
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Inserting blank rows now running very slow

    Thanks for the analysis jolivanes!!

    The If statement can also be slightly refined into a single line which will save more time as follows:

    If Range("A" & lngMyRow) <> Range("A" & lngMyRow).Offset(-1, 0) Then Range("A" & lngMyRow).EntireRow.Insert
    Regards,

    Robert

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Inserting blank rows now running very slow

    Try this one then
    Sub test()
        Dim a, e, n As Long, x As Long, s
        s = Timer
        Application.ScreenUpdating = False
        With Cells(1).CurrentRegion
            a = .Columns(1).Resize(.Rows.Count * 2, 1).Value
            x = .Rows.Count
            With CreateObject("Scripting.Dictionary")
                .CompareMode = 1
                For Each e In a
                    If e <> "" Then
                        If Not .exists(e) Then
                            n = n + 1: .Item(e) = Empty
                            a(x + n, 1) = e
                        End If
                    End If
                Next
            End With
            With .Resize(x + n, .Columns.Count + 1)
                .Columns(.Columns.Count).Value = a
                .Sort .Cells(2, .Columns.Count), 1, Header:=xlNo
                .Columns(.Columns.Count).EntireColumn.Delete
            End With
        End With
        Application.ScreenUpdating = True
        MsgBox Timer - s
    End Sub

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

    Re: Inserting blank rows now running very slow

    OK Gentlemen, here is what I got. (see attached as your machine is probably from this century)

    Trebor76 (Post #2) resp 11.70 and 9.86 seconds
    Trebor76 (Post #2, with suggestion fredlo 2010) resp 13.05 and 14.84 seconds
    Trebor76 (Post #10, with suggestion fredlo 2010) resp 16.66 and 18.52 seconds
    jindon (Post #7) resp 27.06 and 29.86 seconds
    jindon (Post #12) resp 0.39 and 0.39 seconds
    kalak (Post #11) resp 0.61 and 0.61 seconds
    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. Macro is running real slow and makes navigating the worksheet really slow after execution.
    By MichWolverines in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2013, 04:29 PM
  2. [SOLVED] very slow macro (delete blank rows)
    By Danielle22 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-19-2013, 04:13 AM
  3. Slow Running Code when Inserting Column
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2013, 04:23 PM
  4. Inserting blank rows
    By tekiert1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2009, 06:35 PM
  5. Inserting Blank rows after every row upto 2500 rows
    By Manju in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-22-2006, 08:00 AM

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