[英]use `union` with (For Each loop) without using array
在下面的代碼上。 我 select 排在條件。
我需要將union用於 select 所有這些具有以下條件的行。
我知道我的代碼可以通過使用 arrays 完全改變,
但是出於學習目的,如何使用 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
請嘗試下一個改編代碼。 創建一個巨大的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
范圍與 sheet UsedRange
相交並僅復制交集...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.