需要For循环的替代代码

时间:2016-08-04 19:23:28

标签: vba excel-vba excel

我在vba中使用下面的代码但是它花了太多时间来运行。报告有8张,每张纸应检查450多行。

Sub forloop()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
lr = Cells(Rows.Count, 3).End(xlUp).Row - 1
For s = 1 To Sheets.Count
    For x = lr To 1 Step -1
        If Cells(x, 2) <> "" Then
        Cells(x, 2).EntireRow.Delete
    Next x
Next s
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
End Sub

您能否建议我采用其他方法快速运行。

1 个答案:

答案 0 :(得分:1)

dim wb as workbook, sht as worksheet, lr as long, r as long

set wb = workbook.open(wbPathHere)

for each sht in wb.worksheets
    lr = sht.cells(sht.rows.count, 3).End(xlUp).row - 1
    for r = lr to 1 step-1
        if sht.cells(r, 2) <> "" Then sht.cells(r, 2).entirerow.delete
    next r    
next sht