什么是无效或零(0)范围? (身份元素)

时间:2017-10-14 12:27:45

标签: excel vba excel-vba range union

什么是Range无效(标识元素),与sum(+)运算符中的0相同?

x + 0 = x

更新

或与乘数(*)运算符中的1相同。

x * 1 = x

我需要创建无效范围,名为rng as,

Dim rng As Range Set rng = ...

因此在Union()函数中,无效:

rangex = Union(rangex, rng)

表示每个范围(例如Union)与上面rangex(零范围)的求和(rng),再次返回该范围(rangex)。

一些例子:

1-数据类型Int中的无效值为:0

数据类型String中的无效值为:""

所以我需要数据类型Range中的无效值。

1 个答案:

答案 0 :(得分:1)

Union不允许任何参数为Nothing。

您可以使用下面的UDF来实现您想要的效果。

有关详细信息,请参阅this

Function Union2(ParamArray Ranges() As Variant) As Range
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Union2
    ' A Union operation that accepts parameters that are Nothing.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim N As Long
        Dim RR As Range
        For N = LBound(Ranges) To UBound(Ranges)
            If IsObject(Ranges(N)) Then
                If Not Ranges(N) Is Nothing Then
                    If TypeOf Ranges(N) Is Excel.Range Then
                        If Not RR Is Nothing Then
                            Set RR = Application.Union(RR, Ranges(N))
                        Else
                            Set RR = Ranges(N)
                        End If
                    End If
                End If
            End If
        Next N
        Set Union2 = RR
    End Function