根据A列

时间:2016-10-27 15:02:00

标签: excel vba excel-vba

我需要清除具有单词" Actual"的行中的特定单元格区域。在A列中,我一直在试验下面有效的代码,但是它非常慢,因为它需要处理三个IF语句以清除不连续的范围B:E,I:J,N:O。有没有人有关于如何将三个if语句组合成一个语句的想法,和/或提供有关如何加速此代码的想法?谢谢你的考虑。

Sub ClearWeeklyActualPHSR()
Dim lr As Long, i As Long
'Purpuse: clears specific cell ranges if column A = "Actual"

Application.ScreenUpdating = False                      'Disable screen refresh
Application.Calculation = xlCalculationManual           'Switch calculation mode to manual (speeds up calculation)

'Counts number of rows in column A
lr = Sheets("Actual Data").Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lr
    'If "Actual" in column A, then clear contents of columns B:E,I:J,N:O for those rows.
    If Sheets("Actual Data").Cells(i, "A") = "Actual" Then Cells(i, "B").Resize(1, 4).ClearContents
    Cells(i, "I").Resize(1, 2).ClearContents
    Cells(i, "N").Resize(1, 2).ClearContents
Next i

Application.ScreenUpdating = True                       'Enable Auto calculation
Application.Calculation = xlCalculationAutomatic        'Switch calculation mode back to auto
End Sub

1 个答案:

答案 0 :(得分:1)

这是未经测试的,但可以作为模板:

Sub ClearWeeklyActualPHSR()
    Dim lr As Long, i As Long
    'Purpuse: clears specific cell ranges if column A = "Actual" - SUB is SLOW

    Application.ScreenUpdating = False                      'Disable screen refresh
    Application.Calculation = xlCalculationManual           'Switch calculation mode to manual (speeds up calculation)

    'Counts number of rows in column A
    With Sheets("Actual Data")
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        For i = 2 To lr
            'If "Actual" in column A, then clear contents of columns B:E,I:J,N:O for those rows.
            If .Cells(i, "A") = "Actual" Then
                Union(.Cells(i, "B").Resize(1, 4), .Cells(i, "I").Resize(1, 2), .Cells(i, "N").Resize(1, 2)).ClearContents
            End If
        Next i
    End With

    Application.ScreenUpdating = True                       'Enable Auto calculation
    Application.Calculation = xlCalculationAutomatic        'Switch calculation mode back to auto
End Sub