Most efficient way to delete row with VBA

前端 未结 4 381
粉色の甜心
粉色の甜心 2021-01-14 12:27

I currently have a macro that I use to delete a record if the ID doesn\'t exist in a list of ID\'s I created from an XML document. It does work like I want it to, however I

4条回答
  •  天涯浪人
    2021-01-14 13:01

    The short answer:

    Use something like

    ActiveSheet.Range(DelStr).Delete
    ' where DelStr = "15:15" if you want to delete row 15
    '              = "15:15,20:20,32:32" if you want to delete rows 15,20 and 32
    

    The long answer:

    Important: If you have ~ 30 / 35 rows to delete, the following code works very efficiently. Beyond which it would throw up an error. For code to handle arbitrary number of rows efficiently see the very long answer below this.

    If you have a function which lets you list out which rows you want to delete, try the code below. This is what I use to very efficiently delete multiple rows with minimum overhead. (the example assumes that you've obtained the rows you need to delete through some program, here I manually feed them in):

    Sub DeleteRows()
        Dim DelRows() As Variant
        ReDim DelRows(1 To 3)
    
        DelRows(1) = 15
        DelRows(2) = 18
        DelRows(3) = 21
    
        '--- How to delete them all together?
    
        Dim i As Long
        For i = LBound(DelRows) To UBound(DelRows)
            DelRows(i) = DelRows(i) & ":" & DelRows(i)
        Next i
    
        Dim DelStr As String
        DelStr = Join(DelRows, ",")
    
        ' DelStr = "15:15,18:18,21:21"
        '           
        '    IMPORTANT: Range strings have a 255 character limit
        '    See the other code to handle very long strings
    
        ActiveSheet.Range(DelStr).Delete
    End Sub
    

    The (very long) efficient solution for arbitrary number of rows and benchmark results:

    Here are the benchmark results obtained by deleting rows (Time in seconds vs. no. of rows).

    The rows are on a clean sheet and contain a volatile formula in the D column from D1:D100000

    i.e. for 100,000 rows, they have a formula =SIN(RAND())

    enter image description here

    The code is long and not too pretty, but it splits the DelStr into 250 character substrings and forms a range using these. Then the new DeleteRng range is deleted in a single operation.

    The time to delete may depend on the contents of the cells. The testing/benchmarking, in congruence with a bit of intuition suggests the following results.

    • Sparse rows/empty cells delete fastest
    • Cells with values take somewhat longer
    • Cells with formulas take even longer
    • Cells which feed into formulas in other cells take longest as their deletion triggers the #Ref reference error.

    Code:

    Sub DeleteRows()
    
        ' Usual optimization
        ' Events not disabled as sometimes you'll need to interrupt
        ' You can optionally keep them disabled
    
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
    
        ' Declarations...
    
        Dim DelRows() As Variant
    
        Dim DelStr As String, LenStr As Long
        Dim CutHere_Str As String
        Dim i As Long
    
        Dim MaxRowsTest As Long
        MaxRowsTest = 1000
    
        ' Here I'm taking all even rows from 1 to MaxRowsTest
        ' as rows to be deleted
    
        ReDim DelRows(1 To MaxRowsTest)
    
        For i = 1 To MaxRowsTest
            DelRows(i) = i * 2
        Next i
    
        '--- How to delete them all together?
    
        LenStr = 0
        DelStr = ""
    
        For i = LBound(DelRows) To UBound(DelRows)
            LenStr = LenStr + Len(DelRows(i)) * 2 + 2
    
            ' One for a comma, one for the colon and the rest for the row number
            ' The goal is to create a string like
            ' DelStr = "15:15,18:18,21:21"
    
            If LenStr > 200 Then
                LenStr = 0
                CutHere_Str = "!"       ' Demarcator for long strings
            Else
                CutHere_Str = ""
            End If
    
            DelRows(i) = DelRows(i) & ":" & DelRows(i) & CutHere_Str
        Next i
    
        DelStr = Join(DelRows, ",")
    
        Dim DelStr_Cut() As String
        DelStr_Cut = Split(DelStr, "!,")
        ' Each DelStr_Cut(#) string has a usable string
    
        Dim DeleteRng As Range
        Set DeleteRng = ActiveSheet.Range(DelStr_Cut(0))
    
        For i = LBound(DelStr_Cut) + 1 To UBound(DelStr_Cut)
            Set DeleteRng = Union(DeleteRng, ActiveSheet.Range(DelStr_Cut(i)))
        Next i
    
        DeleteRng.Delete
    
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub
    

    The code to generate the formulas in a blank sheet is

    Sub FillRandom()
        ActiveSheet.Range("D1").FormulaR1C1 = "=SIN(RAND())"
        Range("D1").AutoFill Destination:=Range("D1:D100000"), Type:=xlFillDefault
    End Sub
    

    And the code to generate the benchmark results above is

    Sub TestTimeForDeletion()
    
            Call FillRandom
    
            Dim Time1 As Single, Time2 As Single
            Time1 = Timer
    
            Call DeleteRows
    
            Time2 = Timer
            MsgBox (Time2 - Time1)
    End Sub
    

    Note: Many thanks to brettdj for pointing out the error which gets thrown when the length of DelStr exceeding 255 characters. It seems to be a known problem and as I painfully found out, it still exists for Excel 2013.

提交回复
热议问题