Constructing a `Range` containing a cell multiple

2019-09-15 05:30发布

I can easily construct a Range in which the same cell appears twice (in this case cell 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

How can I construct a range in which the same cell appears more than twice??

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-15 05:53

I'm not sure if I am inside your head or not with this, but this seems to count and include cell B1 three times and act as an explicit range.

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
查看更多
登录 后发表回答