在下面的代碼上。我根據條件選擇行。
我需要使用聯合來選擇具有以下條件的所有這些行。
我知道我的代碼可以通過使用陣列來完全改變,
但是出于學習的目的,如何使用 union 來執行相同的操作?
提前感謝有用的意見和回答。
Sub Union_Test()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim lastR As Long: lastR = ws.Cells(Rows.Count, 1).End(xlUp).Row
Dim cel As Range, rng As Range, srg As Variant
Set rng = ws.Range("V3:V" & lastR)
For Each cel In rng
If cel.value = "Yes" Then
cel.EntireRow.Select
End If
Next cel
' I need here to use union to select all (cel.EntireRow)
End Sub
uj5u.com熱心網友回復:
請嘗試下一個改編的代碼。Union創建一個巨大的范圍(從整行)是沒有必要/好的。您只能從匹配條件的特定單元格創建它,然后選擇/洗掉/復制它EntireRow:
Sub Union_Test()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim lastR As Long: lastR = ws.cells(rows.count, 1).End(xlUp).row
Dim cel As Range, rng As Range, uRng As Range
Set rng = ws.Range("V3:V" & lastR)
For Each cel In rng
If cel.value = "Yes" Then
If uRng Is Nothing Then
Set uRng = cel
Else
Set uRng = Union(uRng, cel)
End If
End If
Next cel
If Not uRng Is Nothing Then uRng.EntireRow.Select
End Sub
現在,如果要復制這樣的Union范圍,復制匹配特定條件的整行也不好。在這種情況下,您可以將Union范圍與作業表UsedRange相交并僅復制相交...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/448519.html
