+ Reply to Thread
Results 1 to 10 of 10

Macro speed Excel 2010 vs Excel 2013

Hybrid View

  1. #1
    Registered User
    Join Date
    10-17-2013
    Location
    Ardooie
    MS-Off Ver
    Excel 2007
    Posts
    67

    Macro speed Excel 2010 vs Excel 2013

    Hi guys, quick question about speed of a workbook that has VBA code in it.
    The workbook worked fine in Excel 2010, but now that I got an upgrade at work to Office 2013,
    the book takes forever when changing values.

    Any hints to make this code work faster? Or is it a 2010 vs 2013 thing?


    
    Private Sub Worksheet_Change(ByVal Target As Range)
         
         
    Application.EnableEvents = False
    If Target.Column > 20 Then Exit Sub
    On Error GoTo ErrHandler
    Target.Formula = UCase(Target.Formula)
    ErrHandler:
    
    
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c9:i19").Sort Key1:=Range("c9:i19"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c22:i27").Sort Key1:=Range("c22:i27"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c30:i41").Sort Key1:=Range("c30:i41"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c44:i78").Sort Key1:=Range("c44:i78"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c81:i91").Sort Key1:=Range("c81:i91"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c94:i110").Sort Key1:=Range("c94:i110"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c113:i118").Sort Key1:=Range("c113:i118"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c121:i126").Sort Key1:=Range("c121:i126"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c129:i151").Sort Key1:=Range("c129:i151"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c154:i162").Sort Key1:=Range("c154:i162"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c165:i169").Sort Key1:=Range("c165:i169"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c172:i175").Sort Key1:=Range("c172:i175"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
            Range("c178:i180").Sort Key1:=Range("c178:i180"), _
              Order1:=xlAscending, Header:=xlNo, _
              OrderCustom:=1, MatchCase:=False, _
              Orientation:=xlTopToBottom
        End If
    
    
    Dim WF As Object, r As Long, H As Range, U As Range
    Set WF = WorksheetFunction: Application.EnableEvents = False
    Set H = ActiveWorkbook.ActiveSheet.Rows.Find("*", , , , xlByRows, xlPrevious).Offset(5)
    Set U = ActiveWorkbook.ActiveSheet.Rows.Find("*", , , , xlByRows, xlPrevious).Offset(5)
    Application.ScreenUpdating = False
    For r = ActiveWorkbook.ActiveSheet.Rows.Find("*", , , , xlByRows, xlPrevious).Row + 5 To 12 Step -1
    If WF.CountA(Range(Cells(r - 1, 1), Cells(r - 1, 8))) = 0 _
    And WF.CountA(Range(Cells(r, 1), Cells(r, 8))) = 0 Then _
    Set H = Union(H, Cells(r, 1))
    If WF.CountA(Range(Cells(r - 1, 1), Cells(r - 1, 8))) <> 0 Then _
    Set U = Union(U, Cells(r, 1), Cells(r - 1))
    Next r
    H.EntireRow.Hidden = True: U.EntireRow.Hidden = False
    Application.EnableEvents = True:
    
    Application.EnableEvents = True
    
    
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Application.ScreenUpdating = False
    
    Dim cell As Range
    For Each cell In Range("A1:Z250")
    On Error GoTo ErrHandler
    If Len(cell.Comment.Text) > 0 Then
        With cell.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
    Point1:
    On Error GoTo 0
    Next cell
    
    Exit Sub
    
    ErrHandler:
    Resume Point1:
    
    Application.EnableEvents = True
    
    End Sub
    Thx for any advice, will be appreciated !!
    Kind regards
    Kristof

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro speed Excel 2010 vs Excel 2013

    Hello louvaek,

    Try these macros and let me know how they perform.

    Private Sub Worksheet_Change(ByVal Target As Range)
         
        Dim Area    As Range
        Dim H       As Range
        Dim r       As Long
        Dim rng     As Range
        Dim U       As Range
        Dim WF      As Object
         
         
            If Intersect(Target, Range("c:c")) Is Nothing Then Exit Sub
            
            Set rng = Range("c9:i19,c22:i27,c30:i41,c44:i78,c81:i91,c94:i110,c113:i118,c121:i126,c129:i151,c154:i16,c165:i169,c172:i175,c178:i180")
            
            For Each Area In rng.Areas
                Area.Sort Key1:=Area, Order1:=xlAscending, Header:=xlNo, MatchCase:=False, OrderCustom1:=1, Orientation:=xlLeftToRight
            Next Area
            
            Application.EnableEvents = False
            Application.ScreenUpdating = False
            
                Set WF = WorksheetFunction
                Set H = ActiveWorkbook.ActiveSheet.Rows.Find("*", , , , xlByRows, xlPrevious).Offset(5)
                Set U = H
    
                For r = ActiveWorkbook.ActiveSheet.Rows.Find("*", , , , xlByRows, xlPrevious).Row + 5 To 12 Step -1
                    If WF.CountA(Range(Cells(r - 1, 1), Cells(r, 8))) = 0 Then
                        Set H = Union(H, Cells(r, 1))
                    ElseIf WF.CountA(Range(Cells(r - 1, 1), Cells(r - 1, 8))) <> 0 Then
                        Set U = Union(U, Cells(r, 1), Cells(r - 1))
                    End If
                Next r
                
                H.EntireRow.Hidden = True
                U.EntireRow.Hidden = False
                
            Application.EnableEvents = True:
            Application.ScreenUpdating = True
    
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Dim cell    As Range
        Dim rng     As Range
        
            Set rng = Range("A1:Z250")
            If Intersect(Target, rng) Is Nothing Then Exit Sub
            
            Application.ScreenUpdating = False
            
            For Each cell In rng
                If Not cell.Comment Is Nothing Then
                    With cell.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 65535
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                End If
            Next cell
    
            Application.ScreenUpdating = True
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    10-17-2013
    Location
    Ardooie
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Macro speed Excel 2010 vs Excel 2013

    Hey Leith,

    Thanks for taking some time to look at the code and offering a solution.

    My code does 4 things:
    1/ highlight cells that have comments
    2/ change all lowcase values in uppercase
    3/ Sort ranges C:I depending on input in collumn C
    4/ Unhide a new row if value is added in collumn C, or hide a row when a value is deleted

    After replacing the original code with yours, unfortunately all macro's have stopped working (no error, just not doing anything)
    On the bright side, the sheet works very very fast now hahahaha

    To explain number 3 and 4, let me try to give an example:

    C D E F G H I
    9 Jurgen Jurgen Jurgen Jurgen Jurgen Jurgen
    10 Stefano Stefano Stefano Stefano Stefano Stefano
    11 Ron Ron Ron Ron Ron Ron

    Row 12 is an empty line, where user can make new input in collumn C.
    The moment the input is done, C9:C12 is sorted (A to Z) en and a new line is unhiden (row 13).
    (btw collumn D is just empty)

    Hope this helps a bit

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro speed Excel 2010 vs Excel 2013

    Hello louvaek,

    Sorry about the results. I had hoped it would have worked. Without a workbook, I cannot test the macros accurately.

    The macros are shortened versions of the originals you posted. The reason they were slow was due to an extreme amount of redundancy and improper error handling. There was really no reason you should have had errors in this code.

    If you can post a copy of the workbook, that would be fastest way for me to test and troubleshoot the macros. Before posting a copy of the workbook, please remember to redact any sensitive or confidential information.

  5. #5
    Registered User
    Join Date
    10-17-2013
    Location
    Ardooie
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Macro speed Excel 2010 vs Excel 2013

    One sheet attached (B2019.xlsm)

    This is with the original code that is now so slow in Excel 2013.

    Thanks man, appreciate it !!
    Attached Files Attached Files

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro speed Excel 2010 vs Excel 2013

    Try this (abomination?)

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim SR As Range
         
    Application.EnableEvents = False
    If Target.Column > 20 Then Exit Sub
    On Error GoTo ErrHandler
    Target.Formula = UCase(Target.Formula)
    ErrHandler:
    
    On Error Resume Next
        If Not Intersect(Target, Range("c:c")) Is Nothing Then
        Set SR = Range("c9:i19"): GoSub SortSR
        Set SR = Range("c22:i27"): GoSub SortSR
        Set SR = Range("c30:i41"): GoSub SortSR
        Set SR = Range("c44:i78"): GoSub SortSR
        Set SR = Range("c81:i91"): GoSub SortSR
        Set SR = Range("c94:i110"): GoSub SortSR
        Set SR = Range("c113:i118"): GoSub SortSR
        Set SR = Range("c121:i126"): GoSub SortSR
        Set SR = Range("c129:i151"): GoSub SortSR
        Set SR = Range("c154:i162"): GoSub SortSR
        Set SR = Range("c165:i169"): GoSub SortSR
        Set SR = Range("c172:i175"): GoSub SortSR
        Set SR = Range("c178:i180"): GoSub SortSR
        End If
    
    Dim WF As Object, r As Long, H As Range, U As Range
    Set WF = WorksheetFunction: Application.EnableEvents = False
    Set H = ActiveWorkbook.ActiveSheet.Rows.Find("*", , , , xlByRows, xlPrevious).Offset(5)
    Set U = ActiveWorkbook.ActiveSheet.Rows.Find("*", , , , xlByRows, xlPrevious).Offset(5)
    Application.ScreenUpdating = False
    For r = ActiveWorkbook.ActiveSheet.Rows.Find("*", , , , xlByRows, xlPrevious).Row + 5 To 12 Step -1
    If WF.CountA(Range(Cells(r - 1, 1), Cells(r - 1, 8))) = 0 _
    And WF.CountA(Range(Cells(r, 1), Cells(r, 8))) = 0 Then _
    Set H = Union(H, Cells(r, 1))
    If WF.CountA(Range(Cells(r - 1, 1), Cells(r - 1, 8))) <> 0 Then _
    Set U = Union(U, Cells(r, 1), Cells(r - 1))
    Next r
    H.EntireRow.Hidden = True: U.EntireRow.Hidden = False
    Application.EnableEvents = True:
    
    Application.EnableEvents = True
                        Exit Sub
    SortSR:
    SR.Sort Key1:=SR, Order1:=xlAscending, Header:=xlNo, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
                Return
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  7. #7
    Registered User
    Join Date
    10-17-2013
    Location
    Ardooie
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Macro speed Excel 2010 vs Excel 2013

    Xladept thanks man, works like a charm !!

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro speed Excel 2010 vs Excel 2013

    Hello louvaek,

    I downloaded the workbook you posted. However, something is amiss with the internal structure. The workbook fails to run correctly even with your original macros and froze my system twice.

    Due to the aberrant behavior of the workbook, I am unable to help you further.

  9. #9
    Registered User
    Join Date
    10-17-2013
    Location
    Ardooie
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Macro speed Excel 2010 vs Excel 2013

    Leith, thanks anyway, glad you've tried and appreciate it !

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro speed Excel 2010 vs Excel 2013

    You're welcome and thanks for the rep!

    *Very interesting - the code is, virtually, the same only without so many If.......End If parts

+ 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 works under Excel 2010 but not Excel 2013
    By Carrfamily in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-09-2017, 07:08 AM
  2. Pdf Converting Macro Excel 2010 Working 2013 Not
    By ofd2008 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-15-2017, 02:15 AM
  3. Macro that works in Excel 2010 does not work in Excel 2013
    By karenmwhaley in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2016, 08:12 PM
  4. [SOLVED] Macro to move a row works in excel 2010 but not in Excel 2013
    By mmccra2858 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-31-2015, 05:07 PM
  5. VBA formatting macro that worked in Excel 2010 is not working correctly in Excel 2013
    By jayar2112 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-10-2015, 01:14 PM
  6. Macro code working fine in excel 2013 but not in excel 2010.
    By sere in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-10-2014, 02:23 AM
  7. Replies: 4
    Last Post: 04-10-2014, 12:11 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