+ Reply to Thread
Results 1 to 13 of 13

Macro has long runningtime

Hybrid View

  1. #1
    Registered User
    Join Date
    06-29-2016
    Location
    Aarhus, Denmark
    MS-Off Ver
    2013 and 2016
    Posts
    23

    Macro has long runningtime

    Hello Everyone.

    I am using the following macro to add information of active assignments to a new sheet. HOWEVER the macro takes more then 15 seconds to run. Is there any way of shortening this?


    I appreciate any help. Thanks in Advance

    Cheers,

    Mads
    Sub AddActive()
    
    
                
        Dim Rng As Range
           Dim i As Long
    
        'Looping range
        Set Rng = Sheets("WorkingSheet").Range("AA11:AA1008")
        
        For Each cell In Rng
        
    
               If cell.value = "NO" Then
               
                'write to adjacent cell
                Sheets("Overview").Range("A35:A1000").End(xlDown).Offset(1, 0) = cell.Offset(0, -24).value
                Sheets("Overview").Range("A35:A1000").End(xlDown).Offset(0, 1) = cell.Offset(0, -23).value
                Sheets("Overview").Range("A35:A1000").End(xlDown).Offset(0, 2) = cell.Offset(0, -22).value
                Sheets("Overview").Range("A35:A1000").End(xlDown).Offset(0, 3) = cell.Offset(0, -21).value
                Sheets("Overview").Range("A35:A1000").End(xlDown).Offset(0, 4) = cell.Offset(0, -20).value
                Sheets("Overview").Range("A35:A1000").End(xlDown).Offset(0, 5) = cell.Offset(0, -18).value
                
                                      
                
                
            End If
        Next
    End Sub

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro has long runningtime

    How about this?
    Sub AddActive()
        Dim Rng As Range
        Dim i As Long
        Application.ScreenUpdating = False
        'Looping range
        Set Rng = Sheets("WorkingSheet").Range("AA11:AA1008")
        
        With Sheets("Overview")
            For Each cell In Rng
                If cell.Value = "NO" Then
                   
                    'write to adjacent cell
                    .Cells(1001, "A").End(xlUp).Offset(1).Resize(, 6).Value = _
                        Array(cell.Offset(0, -24).Value, cell.Offset(0, -23).Value, cell.Offset(0, -22).Value, _
                        cell.Offset(0, -21).Value, cell.Offset(0, -20).Value, cell.Offset(0, -18).Value)
                    
                End If
            Next
        End With
        
        Application.ScreenUpdating = True
    End Sub
    多么想要告诉你 我好喜欢你

  3. #3
    Registered User
    Join Date
    06-29-2016
    Location
    Aarhus, Denmark
    MS-Off Ver
    2013 and 2016
    Posts
    23

    Re: Macro has long runningtime

    That's perfect Millz! Thanks a lot! (only 2-3 seconds)
    The macro is a part of multiple macros i run at the same time.
    I also have this macro which takes about 10 seconds. Any way of shortening this?

    Thanks in advance
    Cheers,
    Sub Duplicate()
    
     
        Dim objMyUniqueEntries As Object
        Dim lngRowStart As Long, _
            lngRowEnd As Long, _
            lngMyRow As Long, _
            lngMyCounter As Long
        Sheets("overview").Select
        Set objMyUniqueEntries = CreateObject("Scripting.Dictionary")
        lngRowStart = 36 'Starting row number for the data. Change to suit.
        lngRowEnd = Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
        
        Application.ScreenUpdating = False
        
        For lngMyRow = lngRowEnd To lngRowStart Step -1
            If objMyUniqueEntries.exists(CStr(Range("A" & lngMyRow))) = False Then
                lngMyCounter = lngMyCounter + 1
                objMyUniqueEntries.Add CStr(Range("A" & lngMyRow)), lngMyCounter
            Else
                Rows(lngMyRow).EntireRow.Delete
            End If
        Next lngMyRow
        
        Set objMyUniqueEntries = Nothing
        
        Application.ScreenUpdating = True
        
    End Sub

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro has long runningtime

    You may try the remove duplicates function in Excel itself. If this doesn't work then let's modify your existing macro.


    Sub Duplicate()
    
         'Change "G" to the last column of data you have.
        Sheets("overview").Range("A36:G" & Sheets("overview").Range("A" & rows.count).End(xlUP).row).RemoveDuplicates Columns:=1, Header:=xlYes
        
    End Sub

  5. #5
    Registered User
    Join Date
    06-29-2016
    Location
    Aarhus, Denmark
    MS-Off Ver
    2013 and 2016
    Posts
    23

    Re: Macro has long runningtime

    Hi Millz,

    That option doesn't seem to work.

    If you have any other solution please let me know
    Cheers

  6. #6
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro has long runningtime

    Were there any errors?

    How many rows of data is (usually) involved in the removal of duplicates?

    Would the number of rows ever exceed 32000?

  7. #7
    Registered User
    Join Date
    06-29-2016
    Location
    Aarhus, Denmark
    MS-Off Ver
    2013 and 2016
    Posts
    23

    Re: Macro has long runningtime

    I'm not sure how to use the new line of code you gave me.

    The data looks like this and is typically 100-400 rows
    Data.PNG

  8. #8
    Registered User
    Join Date
    06-29-2016
    Location
    Aarhus, Denmark
    MS-Off Ver
    2013 and 2016
    Posts
    23

    Re: Macro has long runningtime

    This is the error i get:
    Error.PNG

  9. #9
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro has long runningtime

    Quote Originally Posted by madsklavsen View Post
    This is the error i get:
    Attachment 470439
    The error is because you have entered the code wrongly.

    Try this, just copy and paste the whole code:
    Sub Duplicate()
    
         'Change "G" to the last column of data you have.
        Sheets("overview").Range("A35:H" & Sheets("overview").Range("A" & rows.count).End(xlUP).row).RemoveDuplicates Columns:=1, Header:=xlYes
        
    End Sub

  10. #10
    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 long runningtime

    Hi,

    It would likely be considerably faster to use an array rather than reading each cell and further to delete all relevant rows at once.

    Sub DeDuplicate()
    
        Dim objMyUniqueEntries    As Object
        Dim vtEntries             As Variant
        Dim lngRowStart           As Long, _
            lngRowEnd As Long, _
            lngMyRow As Long, _
            lngMyCounter As Long, _
            lngOffset As Long, _
            ix As Long
        Dim RowsToDelete          As Range
    
        Set objMyUniqueEntries = CreateObject("Scripting.Dictionary")
        lngRowStart = 36    'Starting row number for the data. Change to suit.
        lngOffset = lngRowStart - 1
    
        Application.ScreenUpdating = False
        With Sheets("overview")
            lngRowEnd = .Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            vtEntries = .Range(.Cells(lngRowStart, "A"), .Cells(lngRowEnd, "A")).Value2
            For ix = UBound(vtEntries) To LBound(vtEntries) Step -1
    
                If Not objMyUniqueEntries.exists(vtEntries(ix, 1)) Then
                    objMyUniqueEntries.Add vtEntries(ix, 1), Empty
                Else
                    If RowsToDelete Is Nothing Then
                        Set RowsToDelete = .Rows(ix + lngOffset)
                    Else
                        Set RowsToDelete = Union(RowsToDelete, .Rows(ix + lngOffset))
                    End If
                End If
            Next ix
        End With
        
        If Not RowsToDelete Is Nothing Then RowsToDelete.EntireRow.Delete
        
        Set objMyUniqueEntries = Nothing
    
        Application.ScreenUpdating = True
    
    End Sub
    Last edited by xlnitwit; 07-15-2016 at 04:01 AM. Reason: Amended per original specification
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  11. #11
    Registered User
    Join Date
    06-29-2016
    Location
    Aarhus, Denmark
    MS-Off Ver
    2013 and 2016
    Posts
    23

    Re: Macro has long runningtime

    Okay thanks.

    However, i find that this is just a simple "remove duplicates" formula.
    What the beforestated macro does is delete the row of the duplicate AND delete the first appearance of a duplicate instead of the last. Both of these features are vital.

    Please let me know if you have any ideas. Thanks!

  12. #12
    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 long runningtime

    Quote Originally Posted by madsklavsen View Post
    What the beforestated macro does is delete the row of the duplicate AND delete the first appearance of a duplicate instead of the last. Both of these features are vital.

    Please let me know if you have any ideas. Thanks!
    I have corrected the suggestion I made earlier to loop backwards.

  13. #13
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro has long runningtime

    I see, did not know that there's a requirement to keep only the last duplicate record.

    This solution replaces with values (no deletion of rows), so it could be faster:
    Sub Duplicate()
    
        Dim objMyUniqueEntries As Object
        Dim lngRowStart As Long, _
            lngRowEnd As Long, _
            lngMyRow As Long, _
            lngMyCounter As Long
        Dim a, b, i As Long
        Sheets("overview").Select
        Set objMyUniqueEntries = CreateObject("Scripting.Dictionary")
        lngRowStart = 36 'Starting row number for the data. Change to suit.
        lngRowEnd = Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
    
        Application.ScreenUpdating = False
    
        With Sheets("overview").Range("A" & lngRowStart & ":H" & lngRowEnd)
            a = .Value
            For lngMyRow = UBound(a, 1) To LBound(a, 1) Step -1
                If Not objMyUniqueEntries.Exists(a(lngMyRow, 1)) And a(lngMyRow, 1) <> "" Then
                    lngMyCounter = lngMyCounter + 1
                    objMyUniqueEntries.Item(a(lngMyRow, 1)) = 1
                End If
            Next lngMyRow
            ReDim b(1 To lngMyCounter, 1 To UBound(a, 2))
            For lngMyRow = UBound(a, 1) To LBound(a, 1) Step -1
                If objMyUniqueEntries.Exists(a(lngMyRow, 1)) Then
                    objMyUniqueEntries.Remove (a(lngMyRow, 1))
                    For i = LBound(a, 2) To UBound(a, 2)
                        b(lngMyCounter, i) = a(lngMyRow, i)
                    Next
                    lngMyCounter = lngMyCounter - 1
                End If
            Next lngMyRow
            .ClearContents
            .Resize(UBound(b, 1)).Value = b
        End With
    
        Set objMyUniqueEntries = Nothing
    
        Application.ScreenUpdating = True
    
    End Sub

+ 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. Problem with recording a macro with long formula - works in Excel but not in macro
    By tonybeo2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-28-2015, 11:25 AM
  2. Replies: 4
    Last Post: 09-16-2015, 08:07 AM
  3. [SOLVED] =IF and =SUMIF formulas creating long long long data processing times.
    By comp in forum Excel Programming / VBA / Macros
    Replies: 32
    Last Post: 03-26-2014, 02:59 PM
  4. Too Long of a formula for a macro?
    By melnemac32 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-08-2013, 02:30 PM
  5. Macro String too long
    By sam8114 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-25-2013, 12:17 PM
  6. Very long macro - Thanks
    By E3iron in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-09-2010, 01:00 PM
  7. Macro too long
    By John21 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-14-2006, 12:15 PM

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