删除行代码vba

时间:2014-01-22 18:00:32

标签: vba excel-vba optimization excel

以下代码导致我的代码在运行时陷入困境大约15秒。有没有人有任何可以加快这一点的建议?

谢谢,

Range("Test_Range").Offset(1, 1).Activate
Do Until ActiveCell.Offset(0, -1) = ""
    If ActiveCell.Value <= 0.01 Then
        Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 8)).Delete Shift:=xlUp
        ActiveCell.Offset(-1, 0).Activate
    Else
    End If
    ActiveCell.Offset(1, 0).Activate
Loop

2 个答案:

答案 0 :(得分:2)

我会这样做:

'***This code is to be inserted within the coding module
'of the sheet to be modified

Dim calcDefaultState As XlCalculation
'To retain the current XlCalculation property
calcDefaultState = Application.Calculation

'to speed up the process
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

Dim lastRow As Long
'To find the last non empty row of "Test_Range" column
lastRow = Me.Cells(Me.Rows.Count, Range("Test_Range").Column).End(xlUp).Row

Dim i As Long: i = 1
Do Until i = (lastRow - Range("Test_Range").Row) + 1
    With Range("Test_Range").Offset(i, 1)
        If .Value <= .01 Then
            Me.Range(Cells(.Row, 1), Cells(.Row, 8)).Delete Shift:=xlUp
            lastRow = lastRow - 1
        Else
            i = i + 1
        End If
    End With
Loop

'To put back the original XlCalculation property
Application.Calculation = calcDefaultState
Application.ScreenUpdating = True

请注意,如果excel文件的最后一行可能非空,则应添加一项检查以验证它,因为在这种情况下lastRow将不准确。< / p>

答案 1 :(得分:0)

代码在我的计算机上完美而快速地运行。假设您的计算机没有执行问题(即HDD已满或内存不足),那么被删除的范围是否实际上与其他电子表格计算有关?