我目前正在使用我在stackxchg上找到的下面的代码片段来删除那些在A列中没有数字值的行。但是对于包含5000行的工作表而言,它的运行速度非常慢。有什么方法可以让这件事变得更快吗?这个概念是,我有一些行只有在满足条件时才会启动日期,并且将使用此列中的日期生成图表。我希望图表范围引用随行更改,但这很难,因为行中的公式一直向下(对于图表看起来很好,行需要完全为空)。我的解决方法是找到一个可以删除这些行的宏(但使用此代码的速度太慢)。任何帮助,将不胜感激。
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
答案 0 :(得分:2)
你可以在循环结束时进行一次删除:
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
答案 1 :(得分:0)
你可以试试这个
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
您可能要考虑在整理后不删除“非数字”行,只是将第1行中的图表范围引用定义为A列中的最后一行而不是