+ Reply to Thread
Results 1 to 27 of 27

Macro has suddenly started to run slow and consumer all resources.

Hybrid View

taylorsm Macro has suddenly started to... 09-21-2016, 01:38 PM
EssoExplJoe Re: Macro has suddenly... 09-21-2016, 02:18 PM
taylorsm Re: Macro has suddenly... 09-21-2016, 03:07 PM
jindon Re: Macro has suddenly... 09-21-2016, 09:09 PM
taylorsm Re: Macro has suddenly... 09-23-2016, 11:17 AM
EssoExplJoe Re: Macro has suddenly... 09-23-2016, 02:52 PM
jindon Re: Macro has suddenly... 09-24-2016, 12:49 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 09:46 AM
jindon Re: Macro has suddenly... 09-26-2016, 09:50 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 10:30 AM
xlnitwit Re: Macro has suddenly... 09-26-2016, 10:40 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 10:44 AM
xlnitwit Re: Macro has suddenly... 09-26-2016, 10:56 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 11:08 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 11:10 AM
xlnitwit Re: Macro has suddenly... 09-26-2016, 11:11 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 11:16 AM
xlnitwit Re: Macro has suddenly... 09-26-2016, 11:19 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 11:25 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 11:22 AM
xlnitwit Re: Macro has suddenly... 09-26-2016, 11:26 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 11:29 AM
xlnitwit Re: Macro has suddenly... 09-26-2016, 11:30 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 11:37 AM
EssoExplJoe Re: Macro has suddenly... 10-01-2016, 10:28 AM
taylorsm Re: Macro has suddenly... 09-26-2016, 11:32 AM
xlnitwit Re: Macro has suddenly... 09-26-2016, 11:51 AM
  1. #1
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Macro has suddenly started to run slow and consumer all resources.

    I can not think of anything I have changed in the code. The code splits a spread sheet up into 240 separate but similar sheets. It use to take less than 5 minutes, now i've let it run an hour and nothing, or the tabs will be there like it has done them, but I can't verify the status because I can't check for 240 tabs since the sheet is locked up with the macro running. Same laptop running windows 7 with 16gb of ram and a I7 processor. So the power is there. I close out of all programs other than excel too. It is even the same file that I did last that was successful.


    Option Explicit
    
    Sub EDNotepadSplit()
        Dim myAreas As Areas, I As Long, ii As Long, n As Long, rng As Range
        Application.ScreenUpdating = False
        Set myAreas = ActiveSheet.Columns(1).SpecialCells(2).Areas
        For I = 1 To myAreas.Count
            If myAreas(I)(1).Font.Bold Then
                Set rng = myAreas(I).Resize(myAreas(I).Count + 1)
                ii = 1
                Do While I + ii <= myAreas.Count
                    If myAreas(I + ii)(1).Font.Bold Then Exit Do
                    Set rng = Union(rng, myAreas(I + ii))
                    ii = ii + 1
                Loop
                n = n + 1: If "Sheet" & n = myAreas.Parent.Parent.Name Then n = n + 1
                DeleteSheet "Sheet" & n
                myAreas.Parent.Parent.Copy after:=Sheets(Sheets.Count)
                With ActiveSheet
                    .Name = "Sheet" & n: .Cells.ClearContents
                    rng.EntireRow.Copy .Cells(1)
                End With
                I = I + ii - 1: Set rng = Nothing
            End If
        Next
        myAreas.Parent.Parent.Select
        Application.ScreenUpdating = True
    End Sub
    
    Private Sub DeleteSheet(ByVal wsName As String)
        On Error Resume Next
        Application.DisplayAlerts = False
        Sheets(wsName).Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
    End Sub

  2. #2
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    274

    Re: Macro has suddenly started to run slow and consumer all resources.

    I didn't test your code but I don't think its wise to reset the counter in a for/next loop from within the loop. check out your statement "I = I + ii - 1" You can easily get into an endless loop this way.

  3. #3
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Thank you for the response. I guess I should have mentioned that I did not write the code and while I am learning and can fumble through some minor adjustments, the part you referenced is absolutely greek to me. Could you elaborate on what you mean?

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

    Re: Macro has suddenly started to run slow and consumer all resources.

    Seems I wrote the code. (Althoug, I never use variable "I" for a counter, always "i")

    Just try add one line
                With ActiveSheet
                    .Name = "Sheet" & n: .Cells.ClearContents
                    rng.EntireRow.Copy .Cells(1)
                    Application.CutCopyMode = False
                End With
    Otherwise, need to see your workbook.

  5. #5
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Hey thanks, that did not seem to speed it up or from what I could tell, change anything. I've attached a short sample.

    I start off with the "Packet" sheet and then run the macro, resulting in Sheet1-240. So if you want to walk through it just run my macro.

    I wiped all the personal data and then duplicated them over and over and luckily pasted randomly 240 accounts. Ran the macro and it took less than a minute. So something about the content seems to affect it?
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    274

    Re: Macro has suddenly started to run slow and consumer all resources.

    I have found that formatting cell by cell takes a lot more time than just copying cell to cell. I see that in your code you read and re-format cells.

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

    Re: Macro has suddenly started to run slow and consumer all resources.

    Once it create 240 sheets with format(it was the request from the original question), the size goes up to about 70MB from only 340KB.
    Then whatever you want to do, it will be very slow.

    This will create new workbook, so original workbook will never be getting bigger.
    Sub test()
        Dim myAreas As Areas, i As Long, ii As Long, n As Long, rng As Range
        Application.ScreenUpdating = False
        With Workbooks.Add
            ThisWorkbook.Sheets("Packet Detail Report").Copy before:=.Sheets(1)
            Set myAreas = .Sheets("Packet Detail Report").Columns(1).SpecialCells(2).Areas
            For i = 1 To myAreas.Count
                If myAreas(i)(1).Font.Bold Then
                    Set rng = myAreas(i).Resize(myAreas(i).Count + 1)
                    ii = 1
                    Do While i + ii <= myAreas.Count
                        If myAreas(i + ii)(1).Font.Bold Then Exit Do
                        Set rng = Union(rng, myAreas(i + ii))
                        ii = ii + 1
                    Loop
                    n = n + 1
                    If n > .Sheets.Count - 1 Then .Sheets.Add after:=.Sheets(.Sheets.Count)
                    With .Sheets(.Sheets.Count)
                        .Name = "Sheet" & n
                        myAreas.Parent.Parent.Cells.Copy .Cells(1)
                        .Cells.ClearContents
                        rng.EntireRow.Copy .Cells(1)
                    End With
                    i = i + ii - 1: Set rng = Nothing
                End If
            Next
        End With
        myAreas.Parent.Parent.Select
        Application.ScreenUpdating = True
    End Sub

  8. #8
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    I get a subscription out of range error and it highlights ThisWorkbook.

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

    Re: Macro has suddenly started to run slow and consumer all resources.

    .........................
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Man I don't get it. It works in that workbook but mine it does not. The data/format is the same. I simply Control+F each individual column and replaced any cell with data with the other words. So why would one work and the other not?

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro has suddenly started to run slow and consumer all resources.

    Hi,

    If the error occurs on this line
    ThisWorkbook.Sheets("Packet Detail Report").Copy before:=.Sheets(1)
    the most likely reason is that the workbook which contains the code does not have a sheet called "Packet Detail Report" within it. You might start by checking for spelling differences and leading or trailing spaces in the sheet name.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  12. #12
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Nope same spelling, my test workbook is the one that I created the example workbook from. I even copied the workbook name and pasted it to be sure. I then copied the name from the macro and pasted it into the sheet name. Same result.

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro has suddenly started to run slow and consumer all resources.

    And the sheet is in the same workbook as the code?

  14. #14
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    No. All my codes are saved in my PERSONAL.XLSB file. Since the source file changes every month.

  15. #15
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    I did however "copy/move" the workbook into yours and it worked and quickly. So the code is awesome! if we can just get it to run through my PERSONAL.XLSB

  16. #16
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro has suddenly started to run slow and consumer all resources.

    That is the issue. ThisWorkbook always refers to the workbook that contains the code. You must use either:
    ActiveWorkbook.Sheets("Packet Detail Report").Copy before:=.Sheets(1)
    or specify the actual workbook name in a call to the Workbooks collection.

  17. #17
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Damn man, I hate being difficult. I deleted the words ThisWorkbook and typed in ActiveWorkbook and still got the same error

  18. #18
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro has suddenly started to run slow and consumer all resources.

    Was the correct workbook active when you ran the code again?

  19. #19
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Here is the workbook. All I did was replace the words with placeholds. Now I get the "out of range" error with the new text.
    Attached Files Attached Files

  20. #20
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Yes, only have one workbook open, now I get a 1004 error, Method 'Add' of object 'Workbooks' failed

  21. #21
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro has suddenly started to run slow and consumer all resources.

    I would recommend that you restart Excel and then try the code again. You should not be getting errors from basic lines like Workbooks.Add!

  22. #22
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Back to "out of range"

  23. #23
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro has suddenly started to run slow and consumer all resources.

    Please try this one

    Sub test()
        Dim myAreas As Areas, i As Long, ii As Long, n As Long, rng As Range, wb as workbook
        Application.ScreenUpdating = False
        Set wb = activeworkbook
        With Workbooks.Add
            wb.Sheets("Packet Detail Report").Copy before:=.Sheets(1)
            Set myAreas = .Sheets("Packet Detail Report").Columns(1).SpecialCells(2).Areas
            For i = 1 To myAreas.Count
                If myAreas(i)(1).Font.Bold Then
                    Set rng = myAreas(i).Resize(myAreas(i).Count + 1)
                    ii = 1
                    Do While i + ii <= myAreas.Count
                        If myAreas(i + ii)(1).Font.Bold Then Exit Do
                        Set rng = Union(rng, myAreas(i + ii))
                        ii = ii + 1
                    Loop
                    n = n + 1
                    If n > .Sheets.Count - 1 Then .Sheets.Add after:=.Sheets(.Sheets.Count)
                    With .Sheets(.Sheets.Count)
                        .Name = "Sheet" & n
                        myAreas.Parent.Parent.Cells.Copy .Cells(1)
                        .Cells.ClearContents
                        rng.EntireRow.Copy .Cells(1)
                    End With
                    i = i + ii - 1: Set rng = Nothing
                End If
            Next
        End With
        myAreas.Parent.Parent.Select
        Application.ScreenUpdating = True
    End Sub

  24. #24
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Hey xlnitwit that worked. Thanks!

  25. #25
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    274

    Re: Macro has suddenly started to run slow and consumer all resources.

    I use the following code to open workbooks and it doesn't allow the workbook to become the active workbook:

    Application.ScreenUpdating = False
          Workbooks.Open wbook
          Application.ScreenUpdating = True
          Windows(wbookname).Visible = False

  26. #26
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro has suddenly started to run slow and consumer all resources.

    Sub test()
        Dim myAreas As Areas, i As Long, ii As Long, n As Long, rng As Range
        Application.ScreenUpdating = False
        With Workbooks.Add
            ActiveWorkbook.Sheets("Packet Detail Report").Copy before:=.Sheets(1)
            Set myAreas = .Sheets("Packet Detail Report").Columns(1).SpecialCells(2).Areas
            For i = 1 To myAreas.Count
                If myAreas(i)(1).Font.Bold Then
                    Set rng = myAreas(i).Resize(myAreas(i).Count + 1)
                    ii = 1
                    Do While i + ii <= myAreas.Count
                        If myAreas(i + ii)(1).Font.Bold Then Exit Do
                        Set rng = Union(rng, myAreas(i + ii))
                        ii = ii + 1
                    Loop
                    n = n + 1
                    If n > .Sheets.Count - 1 Then .Sheets.Add after:=.Sheets(.Sheets.Count)
                    With .Sheets(.Sheets.Count)
                        .Name = "Sheet" & n
                        myAreas.Parent.Parent.Cells.Copy .Cells(1)
                        .Cells.ClearContents
                        rng.EntireRow.Copy .Cells(1)
                    End With
                    i = i + ii - 1: Set rng = Nothing
                End If
            Next
        End With
        myAreas.Parent.Parent.Select
        Application.ScreenUpdating = True
    End Sub

  27. #27
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Macro has suddenly started to run slow and consumer all resources.

    You're welcome. I realised- belatedly- that as soon as the new workbook was created it became the active one and hence the error you were getting.

+ 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. Excel is suddenly running very slow
    By MSP in forum Excel General
    Replies: 13
    Last Post: 03-10-2022, 04:35 PM
  2. Worksheet suddenly became excruciatingly slow
    By ppgab in forum Excel General
    Replies: 5
    Last Post: 02-29-2016, 06:24 PM
  3. Macros Suddenly Slow
    By watkin01 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2011, 12:57 PM
  4. Workbook started running very slow
    By bowhunt24 in forum Excel General
    Replies: 3
    Last Post: 11-16-2011, 12:58 PM
  5. Macro worked for 2 years, suddenly started giving strange results
    By dylanemcgregor in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-21-2009, 04:39 AM
  6. Compile error has suddenly started happening
    By matpj in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-05-2007, 11:38 AM
  7. Error suddenly started occuring
    By Richard in forum Excel General
    Replies: 1
    Last Post: 07-14-2006, 07: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