+ Reply to Thread
Results 1 to 14 of 14

Macro worked, but now crashes my workbook.

Hybrid View

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

    Macro worked, but now crashes my workbook.

    Had a macro built with this sites help and it worked perfectly. Until shortly ago. I haven't adjusted the code at all other than the name. The only thing i've done since working was share the workbook and lock it, but I have since unlocked it and unshared it.

    Macro is as follows:

    Sub CPR()
    Dim dat As Date, dat2 As Date, lr As Long, lc As Long, i As Long, j As Long, x As Long, arr As Variant, sn As Variant
    With Sheets("Result")
        .Range("A4", "XFD1000000").ClearContents
        If .Range("E1") < .Range("B1") Then .Range("E1") = .Range("B1")
        dat = .Range("B1").Value
        If .Range("E1") = "" Then
            dat2 = dat
        Else
            dat2 = .Range("E1").Value
        End If
    End With
    With Sheets("Totals")
        lc = .Cells(3, .Columns.Count).End(xlToLeft).Column
        For i = 1 To lc Step 5
            lr = .Cells(Rows.Count, i).End(xlUp).Row
            arr = .Range(.Cells(3, i), .Cells(lr, i + 4))
            ReDim sn(1 To UBound(arr), 1 To UBound(arr, 2))
            j = 0
            For x = 3 To UBound(arr)
                If arr(x, 1) >= dat And arr(x, 1) <= dat2 Then
                    j = j + 1
                    sn(j, 1) = arr(x, 1)
                    sn(j, 2) = arr(x, 2)
                    sn(j, 3) = arr(x, 3)
                    sn(j, 4) = arr(x, 4)
                    sn(j, 5) = arr(x, 5)
                End If
            Next
            If j > 0 Then Sheets("Result").Cells(4, i).Resize(j, UBound(sn, 2)) = sn
        Next
    End With
    End Sub
    When I 'step into' the macro it breaks when I am on the ".Range("A4", "XFD1000000").ClearContents' and hit F8. That is when I get the spinning wheel.
    Last edited by taylorsm; 01-27-2017 at 11:10 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro worked, but now crashes my workbook.

    I've never seen anyone actually use the last XFD column so that's a first!

    My first thought is that whilst the original workbook was created with an Excel version > 2007 when the 16K columns were first introduced it has now been opened with a version of Excel prior to that which only has 256 columns. i.e. an .xls file

    Why not change that line to

    .UsedRange.ClearContents
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro worked, but now crashes my workbook.

    Hello taylorsm,

    Formula: copy to clipboard
    ...breaks when I am on the ".Range("A4", "XFD1000000").ClearContents' and hit F8. That is when I get the spinning wheel.


    That is a massive amount of Cells to run trough for any System!

    Besides that, it will definitely crush on any System Running on Any Excel version prior to Excel 2007.

    At best, please try the Code below. Maybe it will speed things up a bit.

    Sub CPR()
    Dim dat As Date, dat2 As Date, lr As Long, lc As Long, i As Long, j As Long, x As Long, arr As Variant, sn As Variant
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    With Sheets("Result")
        .Range("A4", "XFD1000000").ClearContents
        If .Range("E1") < .Range("B1") Then .Range("E1") = .Range("B1")
        dat = .Range("B1").Value
        If .Range("E1") = "" Then
            dat2 = dat
        Else
            dat2 = .Range("E1").Value
        End If
    End With
    With Sheets("Totals")
        lc = .Cells(3, .Columns.Count).End(xlToLeft).Column
        For i = 1 To lc Step 5
            lr = .Cells(Rows.Count, i).End(xlUp).Row
            arr = .Range(.Cells(3, i), .Cells(lr, i + 4))
            ReDim sn(1 To UBound(arr), 1 To UBound(arr, 2))
            j = 0
            For x = 3 To UBound(arr)
                If arr(x, 1) >= dat And arr(x, 1) <= dat2 Then
                    j = j + 1
                    sn(j, 1) = arr(x, 1)
                    sn(j, 2) = arr(x, 2)
                    sn(j, 3) = arr(x, 3)
                    sn(j, 4) = arr(x, 4)
                    sn(j, 5) = arr(x, 5)
                End If
            Next
            If j > 0 Then Sheets("Result").Cells(4, i).Resize(j, UBound(sn, 2)) = sn
        Next
    End With
    
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    End Sub
    Regards,
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Macro worked, but now crashes my workbook.

    Building on Richard's and Winons replies:

    if you replace the .clear contents line with:
    Dim r As Range
    
    Set r = ActiveSheet.UsedRange
    Set r = r.Offset(3, 0)
    r.ClearContents
    It will do only as much work as there is to do.

    Also if you put in the

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    remember to undo it afterwards:

    Application.ScreenUpdating = true
    Application.Calculation = xlCalculationAutomatic


    click on the * Add Reputation if this was useful or entertaining.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro worked, but now crashes my workbook.

    Quote Originally Posted by tony h View Post
    Building on Richard's and Winons replies:

    if you replace the .clear contents line with:
    Dim r As Range
    
    Set r = ActiveSheet.UsedRange
    Set r = r.Offset(3, 0)
    r.ClearContents
    Neat. An excellent suggestion

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

    Re: Macro worked, but now crashes my workbook.

    I also noticed the XFD1000000 lines, but I know nothing of Macro to question someone else that wrote the thing, you know?

    Winon, it still crashed with yours.

  7. #7
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Macro worked, but now crashes my workbook.

    You said it crashed. Does it come up with an error message? Or are you, as before, getting the spinning wheel?


    Also when you reply could you
    1. report what happens if you change the line to
    .Range("A4", "D10").ClearContents
    It won't clear the whole area but it would just check a few things.
    2. report how big the file is and whether it seems over large. I suspect that the run of the code previously may have swollen the file which would cause the .usedrange to run slowly as well.

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

    Re: Macro worked, but now crashes my workbook.

    Do you mean replace
    With Sheets("Result")
        .Range("A4", "XFD1000000").ClearContents
        If .Range("E1") < .Range("B1") Then .Range("E1") = .Range("B1")
    with

    With Sheets("Result")
    Dim r As Range
    Set r = ActiveSheet.UsedRange
    Set r = r.Offset(3, 0)
    r.ClearContents
    If .Range("E1") < .Range("B1") Then .Range("E1") = .Range("B1")


    I tried this and it seems to work...

    Sub CPR()
    Dim dat As Date, dat2 As Date, lr As Long, lc As Long, i As Long, j As Long, x As Long, arr As Variant, sn As Variant
    Dim r As Range
    
    Set r = ActiveSheet.UsedRange
    Set r = r.Offset(3, 0)
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    With Sheets("Result")
         r.ClearContents
        If .Range("E1") < .Range("B1") Then .Range("E1") = .Range("B1")
        dat = .Range("B1").Value
        If .Range("E1") = "" Then
            dat2 = dat
        Else
            dat2 = .Range("E1").Value
        End If
    End With
    With Sheets("Totals")
        lc = .Cells(3, .Columns.Count).End(xlToLeft).Column
        For i = 1 To lc Step 5
            lr = .Cells(Rows.Count, i).End(xlUp).Row
            arr = .Range(.Cells(3, i), .Cells(lr, i + 4))
            ReDim sn(1 To UBound(arr), 1 To UBound(arr, 2))
            j = 0
            For x = 3 To UBound(arr)
                If arr(x, 1) >= dat And arr(x, 1) <= dat2 Then
                    j = j + 1
                    sn(j, 1) = arr(x, 1)
                    sn(j, 2) = arr(x, 2)
                    sn(j, 3) = arr(x, 3)
                    sn(j, 4) = arr(x, 4)
                    sn(j, 5) = arr(x, 5)
                End If
            Next
            If j > 0 Then Sheets("Result").Cells(4, i).Resize(j, UBound(sn, 2)) = sn
        Next
    End With
    
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by taylorsm; 01-27-2017 at 12:51 PM.

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

    Re: Macro worked, but now crashes my workbook.

    I think i got it working with the macro I put in the 10:42 post i edited. Thanks!

  10. #10
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro worked, but now crashes my workbook.

    Hello taylorsm,

    ...the 10:42 post...
    It is actually better to quote by Post Number, to avoid time differences across the Globe.

    Regards.

  11. #11
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro worked, but now crashes my workbook.

    Hi taylorsm,

    Glad you finally managed to get your issue resolved.

    Thank you so much for the "Rep"!

    Regards

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

    Re: Macro worked, but now crashes my workbook.

    Ah, yes!

    Post #6 if any future visitors are wondering.

  13. #13
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Macro worked, but now crashes my workbook.

    I am glad it worked for you just a couple of suggestions: one cosmetic and one functional

    Set r = ActiveSheet.UsedRange
    Set r = r.Offset(3, 0)
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    With Sheets("Result")
         r.ClearContents
    with this:
    Notes:
    1. the cosmetic element is changing the position of some of the code
    2. the functional is to ensure that the code uses the named sheet Result. This could be done by changing ActiveSheet to Sheets("Result") or as I have here by letting the .UsedRange pick up the sheet from the with clause.

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    With Sheets("Result")
         Set r = .UsedRange
         Set r = r.Offset(3, 0)
         r.ClearContents

  14. #14
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Macro worked, but now crashes my workbook.

    That's it, my man!!!

+ 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 2007 : Excel Crashes when I re-calculate (F9) Workbook
    By lissa.lourdes in forum Excel General
    Replies: 1
    Last Post: 02-25-2011, 08:41 PM
  2. Excel crashes when macro gets workbook to close
    By Julesdude in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-24-2011, 09:50 AM
  3. Workbook crashes excel when a macro is performed.
    By Phixtit in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 07-05-2010, 03:55 AM
  4. Excel Crashes when saving workbook...
    By Stu M in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2009, 11:07 AM
  5. Excel Workbook crashes
    By cmourati in forum Excel General
    Replies: 3
    Last Post: 12-03-2006, 06:42 PM
  6. Macro crashes "Automation error" during copy of workbook with char
    By Yorch in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-30-2006, 05:10 PM
  7. [SOLVED] Workbook Crashes Excel
    By Val Mehling in forum Excel General
    Replies: 4
    Last Post: 01-22-2005, 03:08 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