在UFT中更改动态数组大小时出错

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

我正在尝试更改动态数组。最初创建一个空数组,然后在每次需要将项添加到数组之前添加1。

Dim PegSubTot()
for i = 1 to end
If value found then
ReDim Preserve PegSubTot(Ubound(PegSubTot) + 1)
PegSubTot(i) = value
endif
next
vba hp-uft
1个回答
0
投票

您需要先设置数组大小,然后才能更改它:

Dim PegSubTot   ' declare as variable
for i = 1 to end ' presuming end is set somewhere?
    If value found then
        If IsArray(PegSubTot) Then ' check if it's an array
            ReDim Preserve PegSubTot(Ubound(PegSubTot) + 1) ' If it is, extend array by 1
        Else
            ReDim PegSubTot(0) ' If it's not redim as a one element array
        End If
        PegSubTot(UBound(PegSubTot)) = value ' Set highest element of array to value
    end if
next

首先,变量不是数组,因此设置为一个元素数组(您不需要保留,因为它是找到的第一个值)。在每个后续循环中,它是一个数组,因此每次都会扩展一个元素,并且该值始终放在数组的ubound元素中。

如果需要进一步澄清,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.