创建一个包含不连续的数字范围VBA的数组

时间:2019-03-19 13:16:29

标签: arrays vba range

我正在尝试找到一个更好的解决方案,然后正确使用,正在尝试将以下范围之间的每个数字写入单个数组,以便稍后循环。

如果帐户范围的数量增加,并且如果必须由我自己以外的其他人来更改,那么我目前如何编写它的问题就是“ Account_Range_Start”和“ Account_Range_End”将很难维护。输入错误。

下面是我目前拥有的

Dim Account_Range_Start As Variant
Dim Account_Range_End As Variant
Dim AR As Integer
Dim Accounts As Variant
Dim Size As Long
Dim i As Long

'Establish ranges start and end
Account_Range_Start = Array(26240000, 26260000, 26290000, 40000000, 40100000, 40500000, 40600000)
Account_Range_End = Array(26249999, 26269999, 26299999, 40099999, 40199999, 40599999, 40699999)


For AR = 0 To UBound(Account_Range_Start)
    'Checks if this is the first time writing to the array
    If IsEmpty(Accounts) Then
        ReDim Accounts(1 To Account_Range_End(AR) - Account_Range_Start(AR) + 1) As Long

        For i = 1 To UBound(Accounts)
        Accounts(i) = Account_Range_Start(AR) + (i - 1)
        Next i
    Else
        Size = UBound(Accounts)

        ReDim Preserve Accounts(1 To Size + Account_Range_End(AR) - Account_Range_Start(AR) + 1) As Long

        For i = Size + 1 To UBound(Accounts)
        Accounts(i) = Account_Range_Start(AR) + (i - Size - 1)
        Next i
    End If

Next AR

0 个答案:

没有答案