假設我在 Column_H 上有很多值。
我需要"Close"在該列上搜索 value= ,如果找到則clear the entire row包含該值。
注意:使用 Autofilter 方法不適用(由于某些原因)。
現在,我將每個回圈用作下面的代碼,它在大范圍內迭代得有點慢。
是否有另一種方法可以在一次拍攝時更快地做到這一點?提前,歡迎任何有用的幫助。
Option Explicit
Option Compare Text
Sub Search_Clear()
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("SheetB")
Dim StatusColumn As Range: Set StatusColumn = ws.Range("H2", ws.Cells(Rows.Count, "H").End(xlUp))
Dim cell As Object
For Each cell In StatusColumn
If cell.Value = "Close" Then cell.EntireRow.Clear
Next cell
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
uj5u.com熱心網友回復:
您可以從兩個方向提高代碼速度。在陣列中進行迭代比在范圍中進行迭代要快,更重要的是,不要一次清除每一行。Union應使用范圍:
Dim arr, rngClear as Range, i as Long
arr = StatusColumn.Value
for i = 1 to Ubound(arr)
if arr(i,1) = "Close" Then
if rngClear Is Nothing Then
set rngClear = StatusColumn.Cells(i)
else
set rngClear = Union(rngClear, StatusColumn.Cells(i))
end if
end if
next i
'then clear them at the end:
If not rngClear Is Nothing Then rngClear.EntireRow.ClearContents
在你昨天的問題中,如果我沒記錯的話,上面的范圍已經迭代了,這意味著不需要第二步(上面的一步)。該Union范圍應在第一次(現有)迭代中創建......
uj5u.com熱心網友回復:
嘗試讀取一整列而不是多個單元格讀取:
Dim StatusValue, i As Long
StatusValue = StatusColumn.Value
For i = LBound(StatusValue) To UBound(StatusValue)
If StatusValue(i, 1) = "Close" Then ws.Rows(i 1).Clear
Next i
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415396.html
標籤:
