Excel 我能做些什么来改进我的VBA代码?
我写了一个VBA宏来自动删除一些行, 不知道为什么它从未结束处理 这可能是我代码中的错误吗 谢谢你的帮助Excel 我能做些什么来改进我的VBA代码?,excel,vba,Excel,Vba,我写了一个VBA宏来自动删除一些行, 不知道为什么它从未结束处理 这可能是我代码中的错误吗 谢谢你的帮助 Sub AutoProcess() Application.ScreenUpdating = False Application.Calculation = xlManual Dim Row, RowCount RowCount = ActiveSheet.UsedRange.Rows.Count For Row = 3 To RowCount If ActiveSheet.
Sub AutoProcess()
Application.ScreenUpdating = False
Application.Calculation = xlManual
Dim Row, RowCount
RowCount = ActiveSheet.UsedRange.Rows.Count
For Row = 3 To RowCount
If ActiveSheet.Cells(Row, 7).Value = 0 And ActiveSheet.Cells(Row, 9).Value = 0 Then
Rows(Row).Delete
RowCount = ActiveSheet.UsedRange.Rows.Count
Row = Row - 1
End If
Next Row
MsgBox ("finished")
Application.Calculate
Application.ScreenUpdating = True
End Sub
尝试使用union来提高速度
Sub Delete_Rows_Using_Loops()
Dim ws As Worksheet, r As Long, c As Range
Application.ScreenUpdating = False
Application.Calculation = xlManual
Set ws = ThisWorkbook.Sheets("Sheet1")
For r = ws.UsedRange.Rows.Count To 3 Step -1
If ws.Cells(r, 7).Value = 0 And ws.Cells(r, 9).Value = 0 Then
If c Is Nothing Then Set c = ws.Rows(r) Else Set c = Union(c, ws.Rows(r))
End If
Next r
If Not c Is Nothing Then c.Delete
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True
MsgBox "Finished", 64
End Sub
另一种更快的方法是使用自动筛选。假设第2行有标题
Sub Delete_Rows_Using_AutoFilter()
Dim ws As Worksheet
Application.ScreenUpdating = False
Application.Calculation = xlManual
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws.Range("G2:I" & ws.Cells(Rows.Count, 7).End(xlUp).Row)
.AutoFilter 1, "="
.AutoFilter 3, "="
If .Columns(1).SpecialCells(12).Count > 1 Then
.Offset(1).Resize(.Rows.Count - 1).Delete xlShiftUp
End If
.AutoFilter
End With
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True
MsgBox "Finished", 64
End Sub
第三种方法是使用Evaluate来获取要删除的行
Sub Delete_Rows_Using_Evaluate()
Dim x, ws As Worksheet, r As Range
Application.ScreenUpdating = False
Application.Calculation = xlManual
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set r = .Range("G3:G" & .Cells(Rows.Count, 7).End(xlUp).Row)
x = Filter(.Evaluate("TRANSPOSE(IF((" & r.Address & "=0)+(" & r.Offset(, 2).Address & "=0),""A"" & ROW(" & r.Address & ")))"), False, False)
If UBound(x) = -1 Then Exit Sub
.Range(Join(x, ",")).EntireRow.Delete
End With
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True
End Sub
您可以使用“helper”列和公式
With Range(Cells(3, 7), Cells(Rows.Count, 7).End(xlUp))
With .Offset(, .Parent.UsedRange.Columns.Count)
.FormulaR1C1 = "=IF(SUM(RC7,RC9)=0,1,"""")"
.SpecialCells(xlCellTypeFormulas, xlNumbers).EntireRow.Delete
.Delete
End With
End With
这将使用过滤器而不是循环。
我还使用了
.Cells(.Rows.Count,1).End(xlUp).Row
而不是UsedRange
,因为这样会返回不正确的结果
Sub Test()
Dim wrksht As Worksheet
Dim lLastRow As Long
Set wrksht = ThisWorkbook.Worksheets("Sheet1")
With wrksht
lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Finds last row in column A containing data.
With .Range("A3", .Cells(lLastRow, 9))
.AutoFilter
.AutoFilter Field:=7, Criteria1:="0"
.AutoFilter Field:=9, Criteria1:="0"
End With
.Range("A4", .Cells(lLastRow, 9)).SpecialCells(xlCellTypeVisible).EntireRow.Delete Shift:=xlUp
.AutoFilterMode = False
End With
End Sub
我从Stack Overflow>>中学到永远不要使用
activesheet
,最好在工作表中使用变量。这真的很有帮助。谢谢,很高兴知道你应该避免使用activesheet。然而,row=row-1是用于,当一行被删除时,我假设下面的所有行都会向上移动一行(可能我错了?需要测试)在上面的代码中,您不需要row=row-1,如果您使用步骤1并从底部向上移动row啊,您是在向后计算!当然,这比使用For循环更好,因此您可以从RowCount
转到0
——这样您就不需要重置RowCount
,也不需要执行Row=Row-1
。代码行数越少通常意味着出现错误的可能性越低。只需再次检查-您是否希望检查“值为0
”,而不是“单元格为空”(ActiveSheet.Cells(第7行)。value=“
)?试着用F8一次遍历一行,看看哪里出了问题,并可能使用Debug.Print行&“:”&ActiveSheet.Cells(行,7.Value&“,”&ActiveSheet.Cells(行,9.Value)
在执行If
之前检查值statement@Enigmativity对这真是个好主意。感谢you@Chronocidal我想一步一步地检查它的0是否为空。
Sub Test()
Dim wrksht As Worksheet
Dim lLastRow As Long
Set wrksht = ThisWorkbook.Worksheets("Sheet1")
With wrksht
lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Finds last row in column A containing data.
With .Range("A3", .Cells(lLastRow, 9))
.AutoFilter
.AutoFilter Field:=7, Criteria1:="0"
.AutoFilter Field:=9, Criteria1:="0"
End With
.Range("A4", .Cells(lLastRow, 9)).SpecialCells(xlCellTypeVisible).EntireRow.Delete Shift:=xlUp
.AutoFilterMode = False
End With
End Sub