[英]Clear specific cell ranges based on text in column A
I need to clear out specific cell ranges in the rows that have the word "Actual" in column A. I have been experimenting with the following code that works, however it is extremely slow because it needs to process through three IF statements to clear out the discontinuous ranges B:E,I:J,N:O. 我需要清除A列中带有“ Actual”字样的行中的特定单元格范围。我一直在尝试以下有效的代码,但是它非常慢,因为它需要处理三个IF语句才能清除不连续范围B:E,I:J,N:O。 Does anyone have ideas as how to combine the three if statements into one statement, and/or provide ideas on how to speed up this code?
是否有人有想法将三个if语句组合为一个语句,和/或提供有关如何加快此代码速度的想法? Thank you for your consideration.
谢谢您的考虑。
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
This is untested , but may serve as a template: 这未经测试 ,但可以用作模板:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.