简体   繁体   English

Excel VBA删除行-如何使其更快?

[英]Excel VBA to delete rows — how to make this quicker?

I am current using the below code snippet i found on stackxchg to delete rows that whereby there is no numeric value in column A. This works however it is gruesomely slow for a sheet with 5000 rows. 我目前正在使用我在stackxchg上找到的以下代码片段删除行,从而在A列中没有数值。这行得通,但是对于具有5000行的工作表来说速度却非常慢。 Is there any way I can get this thing to go faster? 有什么办法可以使这件事更快地进行吗? The concept is, I have some rows that will kick out dates only if a criteria is met, and a chart will be generated using the dates in this column. 概念是,我有一些行仅在满足条件时才会显示日期,并且将使用此列中的日期生成图表。 I would like the chart range reference to change with the rows, but this is tough since there are formulas in the rows all the way down (and for a chart to look good the rows need to be completely empty). 我希望图表范围引用随行而变化,但是这很困难,因为行中的公式一直向下(为了使图表看起来不错,行必须完全为空)。 My workaround was to find a macro which could delete these rows (but it's going too slow using this code). 我的解决方法是找到一个可以删除这些行的宏(但是使用此代码太慢了)。 Any help would be appreciated. 任何帮助,将不胜感激。

Sub Sample()
Dim LR3 As Long, i3 As Long

With Sheets("Basket Performance")
    LR3 = .Range("A" & .Rows.Count).End(xlUp).Row

    For i3 = LR3 To 2 Step -1
        If Not IsNumeric(.Range("A" & i3).Value) Or _
        .Range("A" & i3).Value = "" Then .Rows(i3).Delete
    Next i3
End With

End Sub 结束子

You can do a single delete at the end of your loop: 您可以在循环结束时执行一次删除操作:

Sub Sample()

    Dim LR3 As Long, i3 As Long, rng As Range

    With Sheets("Basket Performance")
        LR3 = .Range("A" & .Rows.Count).End(xlUp).Row

        For i3 = LR3 To 2 Step -1
            If Not IsNumeric(.Range("A" & i3).Value) Or _
                           .Range("A" & i3).Value = "" Then 
                If rng Is Nothing Then
                    Set rng = .Cells(i3, 1)
                Else
                    Set rng = application.union(rng, .Cells(i3, 1))
                End If
            End If '<<EDIT
        Next i3
    End With

    If Not rng Is Nothing then rng.Entirerow.Delete

End Sub

you can try this 你可以试试这个

Option Explicit

Sub delrow()

With ThisWorkbook.Worksheets("Basket Performance")
    .Columns("A").Insert '<== insert a "helper" column for counting and sorting purposes. it'll be removed by the end of the macro
    .Columns("B").SpecialCells(xlCellTypeConstants, xlNumbers).Offset(, -1).FormulaR1C1 = "=COUNT(R1C[1]:RC[1])"
    .Cells.Sort key1:=.Columns("A"), order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlNo
    .Columns("A").Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete '<== maybe you don't need to delete but just define the chart range reference from row 1 down to the last row in column A with a number
    .Columns("A").Delete '<== remove the "helper" column
End With

End Sub

you may want to consider not deleting "non numeric" rows once sorted out, and just defining the chart range reference from row 1 down to the last row in column A with a number instead 您可能要考虑不删除整理后的“非数字”行,而只是使用数字定义从第1行到A列最后一行的图表范围参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM