构造一个包含多次单元格的`Range`

时间:2015-01-09 15:54:51

标签: excel excel-vba vba

我可以轻松地构建一个Range,其中相同的单元格出现两次(在本例中为单元格B1):

Sub IAmTheCount()
    Dim r1 As Range, r2 As Range, r3 As Range
    Set r1 = Range("A1:B1")
    Set r2 = Range("B1:B2")
    Set r3 = Union(r1, r2)
    MsgBox r3.Count
End Sub

如何构建一个范围,其中相同的单元格出现两次以上?

1 个答案:

答案 0 :(得分:3)

我不确定我是否在你的脑海里,但这似乎算上并包括细胞B1三次,并作为一个明确的范围。

Sub IAmTheCount()
    Dim r1 As Range, r2 As Range, r3 As Range, r4 As Range
    Set r1 = Range("A1:B1")
    Set r2 = Range("B1:B5")
    Set r3 = Range("B1:C2")
    Set r4 = Union(r1, r2, r3)
    MsgBox r4.Count & " - " & r4.Address
    r4.Interior.ColorIndex = 4
End Sub