vba:union方法 并集
'Application.Union 方法
'返回两个或多个区域的合并区域
'
Sub test()
Range("a1:b3,c5:d8").Select '文本地址引用方式
Union([a1:b3], [c5:d8]).Select '单元格区域引用方式
End Sub
'小结:虽然range也可以完成多区域的引用
'但文本地址的引用方式最多不能超过256个字符
'而union却没有这个限制
'我们经常利用变量与union进行单元格的连接
Sub 连接符单元格连接()
Dim rng As Range
For Each rngs In [b2:b10]
adss = rngs.Address
ads = ads & rngs.Address & ","
Next
ad = Left(ads, Len(ads) - 1)
End Sub
Sub union单元格连接()
Dim rng As Range, rngs As Range
Set rng = [b2]
For Each rngs In [b2:b10]
adss = rngs.Address
Set rng = Union(rng, rngs)
ads = rng.Address
Next
End Sub
案例
Sub 条件筛选()
For Each rng In Range([b2], Cells(Application.CountA([a:a]), 2))
If rng > 90 Then
k = k + 1
If k = 1 Then
Set rn = rng
Else
Set rn = Union(rn, rng)
aa = rn.Address
End If
End If
Next
For Each rngs In rn
n = n + 1
Cells(n + 1, "d") = Cells(rngs.Row, "a")
Cells(n + 1, "e") = rngs
Next
End Sub