VBA:Application.Union(.......)方法中有超过30个参数

问题描述 投票:0回答:1

在我的 VBA 代码中,我使用方法 Application.Union(....) 选择多个单元格

看下面的代码

        Union(Range(Cells(1, spalte11), Cells(2, spalte11)), Range(Cells(txtBoxRangeAValue + 2, spalte11), Cells(txtBoxRangeBValue + 2, spalte11)), _
          Range(Cells(1, spalte12), Cells(2, spalte12)), Range(Cells(txtBoxRangeAValue + 2, spalte12), Cells(txtBoxRangeBValue + 2, spalte12)), _
          Range(Cells(1, spalte13), Cells(2, spalte13)), Range(Cells(txtBoxRangeAValue + 2, spalte13), Cells(txtBoxRangeBValue + 2, spalte13)), _
          Range(Cells(1, spalte14), Cells(2, spalte14)), Range(Cells(txtBoxRangeAValue + 2, spalte14), Cells(txtBoxRangeBValue + 2, spalte14)), _
          Range(Cells(1, spalte15), Cells(2, spalte15)), Range(Cells(txtBoxRangeAValue + 2, spalte15), Cells(txtBoxRangeBValue + 2, spalte15)), _
          Range(Cells(1, spalte16), Cells(2, spalte16)), Range(Cells(txtBoxRangeAValue + 2, spalte16), Cells(txtBoxRangeBValue + 2, spalte16)), _
          Range(Cells(1, spalte17), Cells(2, spalte17)), Range(Cells(txtBoxRangeAValue + 2, spalte17), Cells(txtBoxRangeBValue + 2, spalte17)), _
          Range(Cells(1, spalte18), Cells(2, spalte18)), Range(Cells(txtBoxRangeAValue + 2, spalte18), Cells(txtBoxRangeBValue + 2, spalte18)), _
          Range(Cells(1, spalte21), Cells(2, spalte21)), Range(Cells(txtBoxRangeAValue + 2, spalte21), Cells(txtBoxRangeBValue + 2, spalte21)), _
          Range(Cells(1, spalte22), Cells(2, spalte22)), Range(Cells(txtBoxRangeAValue + 2, spalte22), Cells(txtBoxRangeBValue + 2, spalte22)), _
          Range(Cells(1, spalte23), Cells(2, spalte23)), Range(Cells(txtBoxRangeAValue + 2, spalte23), Cells(txtBoxRangeBValue + 2, spalte23)), _
          Range(Cells(1, spalte24), Cells(2, spalte24)), Range(Cells(txtBoxRangeAValue + 2, spalte24), Cells(txtBoxRangeBValue + 2, spalte24)), _
          Range(Cells(1, spalte25), Cells(2, spalte25)), Range(Cells(txtBoxRangeAValue + 2, spalte25), Cells(txtBoxRangeBValue + 2, spalte25)), _
          Range(Cells(1, spalte26), Cells(2, spalte26)), Range(Cells(txtBoxRangeAValue + 2, spalte26), Cells(txtBoxRangeBValue + 2, spalte26)), _
          Range(Cells(1, spalte27), Cells(2, spalte27)), Range(Cells(txtBoxRangeAValue + 2, spalte27), Cells(txtBoxRangeBValue + 2, spalte27))).Select

上面我选择了 Application.Union(...)+ 方法中允许的所有 30 个参数

但实际上我想选择超过 30 个范围。

这可能吗?

excel vba arguments union multipleselection
1个回答
0
投票

定义一个包含每个范围的字符串的向量

Dim v() As Variant
ReDim v(n) 'n is number of your desired ranges, more than 30 arguments allowable by a simple Union
For i = 0 To n - 1
    'v(i) = '.... is a string which is the address of each range
Next i

定义一个范围 RR,在此基础上使用 Union 逐步添加范围

Dim RR As Range
For i = 0 To n
    If i = 0 Then
        Set RR = Range(v(i))
    Else
        Set RR = Union(RR, Range(v(i)))
    End If
Next i
RR.Select
© www.soinside.com 2019 - 2024. All rights reserved.