+ Reply to Thread
Results 1 to 20 of 20

Macro runs Slowly

Hybrid View

Mysore Macro runs Slowly 02-05-2012, 11:56 PM
rylo Re: Macro runs Slowly 02-06-2012, 12:22 AM
Mysore Re: Macro runs Slowly 02-06-2012, 01:41 AM
OnErrorGoto0 Re: Macro runs Slowly 02-06-2012, 05:38 AM
rylo Re: Macro runs Slowly 02-06-2012, 05:44 PM
dangelor Re: Macro runs Slowly 02-06-2012, 06:27 PM
Mysore Re: Macro runs Slowly 02-06-2012, 09:17 PM
rylo Re: Macro runs Slowly 02-06-2012, 09:23 PM
Mysore Re: Macro runs Slowly 02-06-2012, 10:32 PM
rylo Re: Macro runs Slowly 02-06-2012, 11:59 PM
Mysore Re: Macro runs Slowly 02-07-2012, 01:11 AM
rylo Re: Macro runs Slowly 02-07-2012, 01:13 AM
Mysore Re: Macro runs Slowly 02-07-2012, 01:23 AM
dangelor Re: Macro runs Slowly 02-07-2012, 08:58 AM
Mysore Re: Macro runs Slowly 02-07-2012, 07:47 PM
dangelor Re: Macro runs Slowly 02-08-2012, 08:55 AM
Mysore Re: Macro runs Slowly 02-07-2012, 08:01 PM
snb Re: Macro runs Slowly 02-07-2012, 11:30 AM
OnErrorGoto0 Re: Macro runs Slowly 02-07-2012, 11:33 AM
rylo Re: Macro runs Slowly 02-07-2012, 08:23 PM
  1. #1
    Forum Contributor
    Join Date
    01-03-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2016,Excel 2013
    Posts
    186

    Macro runs Slowly

    Hi Guys,



    The macro below works perfectly fine. It creates 250 rows in Sheet 1 and copies formula . Then it does the similar

    thing in Sheet 2. However this macro runs slowly. Can any one speed it up?

    Thanks

    Sub InsertRowS1()
    '
        ActiveSheet.Unprotect Password:="team"
    
      '   Stop screen refresh
    '    Application.ScreenUpdating = False
    
    '   Insert Blank Row
        Worksheets("Data Input -Unit Leaders").Activate
        Application.Goto Reference:="R1000000C1"
        Selection.End(xlUp).Select
        ActiveCell.Offset(-1, 0).Select
        Rows(ActiveCell.Row).Select
        
    '   Loop 100 times
        i = 1
        Do
        Rows(ActiveCell.Row).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
    '   Copy Formulas, & paste in blank row
        ActiveCell.Offset(3, 0).Select
        Rows(ActiveCell.Row).Select
        Selection.Copy
        ActiveCell.Offset(-3, 0).Select
        Rows(ActiveCell.Row).Select
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
        ActiveCell.Offset(1, 0).Select
        i = i + 1
        Loop Until i = 250
        ActiveSheet.Protect Password:="team"
        
    '   Insert Blank Row
        Worksheets("Ranking  - Unit Leaders").Activate
        ActiveSheet.Unprotect Password:="team"
        Application.Goto Reference:="R1000000C1"
        Selection.End(xlUp).Select
        ActiveCell.Offset(-1, 0).Select
    
    '   Loop 100 times
        i = 1
        Do
        Rows(ActiveCell.Row).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
    '   Copy Formulas, & paste in blank row
        ActiveCell.Offset(-1, 0).Select
        Rows(ActiveCell.Row).Select
        Selection.Copy
        ActiveCell.Offset(1, 0).Select
        Rows(ActiveCell.Row).Select
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
        ActiveCell.Offset(1, 0).Select
        i = i + 1
        Loop Until i = 250
        ActiveSheet.Protect Password:="team"
    
    '   Start Screen refresh
        Application.ScreenUpdating = True
      
       
    
    End Sub
    Last edited by ConneXionLost; 02-05-2012 at 11:59 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro runs Slowly

    Hi

    Not sure what you have in your formulas, but see how this goes.
    Sub AAA()
      Dim InsertCnt As Long
      InsertCnt = 250
      shtarr = Array("Data Input -Unit Leaders", "Ranking - Unit Leaders")
      For i = LBound(shtarr) To UBound(shtarr)
        Sheets(shtarr(i)).Activate
        ActiveSheet.Unprotect Password:="team"
        actionrow = Cells(Rows.Count, 1).End(xlUp).Offset(-1, 0).Row
        Cells(actionrow, 1).Resize(InsertCnt, 1).EntireRow.Insert shift:=xlDown
        Cells(actionrow + InsertCnt + 2, 1).EntireRow.Copy Destination:=Cells(actionrow, 1).Resize(InsertCnt, 1)
        ActiveSheet.Protect Password:="team"
      Next i
    End Sub
    rylo

  3. #3
    Forum Contributor
    Join Date
    01-03-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2016,Excel 2013
    Posts
    186

    Re: Macro runs Slowly

    Hi Rylo,

    Tried your code but debugs at Sheets (Shtarr(i)).Activate (6th Line)
    I see your code is assuming that both sheets are identical which is not the case. Any help here?

  4. #4
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro runs Slowly

    You appear to have commented out the line that turns off screenupdating. I suggest you uncomment that line and also set calculation to manual at the start and back to automatic at the end. Furthermore all that selecting is unnecessary - you can just manipulate the ranges directly.
    Good luck.

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro runs Slowly

    Hi

    1) check the sheet names. I just took them from your code, but if it isn't right, then it will error.

    2) Missed that difference. You could just duplicate the code and specifically nominate the sheet as you have done, as I think the insertion method will still be faster.

    Better still, put up an example file for review. Makes things much easier to work through.

    rylo

  6. #6
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,308

    Re: Macro runs Slowly

    Not tested, but this should be very quick...
    Sub InsertRowS1()
        
        With Worksheets("Data Input -Unit Leaders")
            .Activate
            .Unprotect Password:="team"
            .Cells(.Rows.Count, 1).End(xlUp).Offset(2, 0).EntireRow.Copy
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-1, 0).EntireRow.Resize(250).Insert shift:=xlDown
            .Protect Password:="team"
        End With
        
        With Worksheets("Ranking - Unit Leaders")
            .Activate
            .Unprotect Password:="team"
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-2, 0).EntireRow.Copy
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-1, 0).EntireRow.Resize(250).Insert shift:=xlDown
            .Protect Password:="team"
        End With
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    01-03-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2016,Excel 2013
    Posts
    186

    Re: Macro runs Slowly

    Hi Dangleor

    Thanks for your input but the code you are suggesting are only inserting rows and does not copy forumuals.
    Please refer to my original code, the macro is inserting and copying formulas only. This it does to both my sheets.
    The macro is absolutely fine but i need a code to speed it up.
    Thanks

  8. #8
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro runs Slowly

    Mysore

    really think we need the example file to work with here....

    rylo

  9. #9
    Forum Contributor
    Join Date
    01-03-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2016,Excel 2013
    Posts
    186

    Re: Macro runs Slowly

    Hi Guys,

    Just Attaching a sample worksheet. I have just revised the macro to loop 2o times for
    illustration purpose but the final version will have 250 rows to add and copy formulas.


    Hope this helps

    Thanks
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro runs Slowly

    Hi

    I've just tweaked dangelor's code a bit and I think it gives you what you are chasing.

    Sub InsertRowS2()
        
        With Worksheets("Data Input -Unit Leaders")
            .Activate
            .Unprotect Password:="team"
            .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow.Copy
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-1, 0).EntireRow.Resize(250).Insert shift:=xlDown
            .Protect Password:="team"
        End With
        
        With Worksheets("Ranking  - Unit Leaders")
            .Activate
            .Unprotect Password:="team"
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-2, 0).EntireRow.Copy
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-1, 0).EntireRow.Resize(250).Insert shift:=xlDown
            .Protect Password:="team"
        End With
    
    End Sub
    rylo

  11. #11
    Forum Contributor
    Join Date
    01-03-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2016,Excel 2013
    Posts
    186

    Re: Macro runs Slowly

    Hi Rylo

    Thanks for the code. Is is certainly faster. Just one hurdle to cross. The Data Input - Sheet is fully protected but permits
    data entry only in specific range. The new rows which are created are locked. My earlier macro did have not this issue.
    Maybe when the formula copies, it should copy the formats as well and this could help,
    Can you tweak the code?

    Thanks

  12. #12
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro runs Slowly

    Hi

    Give some examples of the ranges so we know exactly what / where to check.

    rylo

  13. #13
    Forum Contributor
    Join Date
    01-03-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2016,Excel 2013
    Posts
    186

    Re: Macro runs Slowly

    Hi rylo

    Range A8 - DW308 in the Data Input - Unit Leader Sheet in my original file. Infact you look at my attachment and run the macro.
    This might give u an understadning

  14. #14
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,308

    Re: Macro runs Slowly

    Try...
    Sub InsertRowS3()
    
        With Worksheets("Data Input -Unit Leaders")
            .Activate
            .Unprotect Password:="team"
            With .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
                .Cells.Locked = False
                .Copy
            End With
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-1, 0).EntireRow.Resize(250).Insert shift:=xlDown
            .Protect Password:="team"
        End With
    
        With Worksheets("Ranking  - Unit Leaders")
            .Activate
            .Unprotect Password:="team"
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-2, 0).EntireRow.Copy
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-1, 0).EntireRow.Resize(250).Insert shift:=xlDown
            .Protect Password:="team"
        End With
    
    End Sub

  15. #15
    Forum Contributor
    Join Date
    01-03-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2016,Excel 2013
    Posts
    186

    Re: Macro runs Slowly

    Hi Dangelor,

    I used the above code but it groups the newly inserted rows since the row where it copies the formula is grouped.
    This cannot be ungrouped by the user as the worksheet is protected.
    Please refer to the original file I have attached and the macro does not group the new insertion .

    Appreciate if you can tweak it.

    Thanks

  16. #16
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,308

    Re: Macro runs Slowly

    Tweaked...
    Sub InsertRowS4()
    
        With Worksheets("Data Input -Unit Leaders")
            .Activate
            .Unprotect Password:="team"
            With .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
                .Cells.Locked = False
                .Ungroup
                .Copy
                .Group
            End With
            .Cells(.Rows.Count, 1).End(xlUp).Offset(-1, 0).EntireRow.Resize(250).Insert
            .Protect Password:="team"
        End With
    
            With Worksheets("Ranking  - Unit Leaders")
                .Activate
                .Unprotect Password:="team"
                .Cells(.Rows.Count, 1).End(xlUp).Offset(-2, 0).EntireRow.Copy
                .Cells(.Rows.Count, 1).End(xlUp).Offset(-1, 0).EntireRow.Resize(250).Insert shift:=xlDown
                .Protect Password:="team"
            End With
    
    End Sub

  17. #17
    Forum Contributor
    Join Date
    01-03-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2016,Excel 2013
    Posts
    186

    Re: Macro runs Slowly

    Hi Rylo,

    Did you get a chance to have a look at it?

  18. #18
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Post Re: Macro runs Slowly

    refrain from 'activate'

    Did you consider to use:

    Sub tst()
      With Sheet41.Cells(Rows.Count, 1).End(xlUp).Offset(-1).Resize(, UsedRange.Columns.Count)
        .AutoFill .Resize(200)
      End With
    End Sub



  19. #19
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro runs Slowly

    @snb,
    Usedrange needs a sheet.

  20. #20
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro runs Slowly

    Hi

    You will have to change the row that you are copying (on the first sheet row 311 in the example file) to have to correct cell permissions. Otherwise, you will have to change the row that you want to use as the base row to copy. Given you have drop downs as well, it may pay to put those into the main copy row as well.

    rylo

+ 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