vba:循环用户定义的数据类型

时间:2021-10-24 16:25:24

I have this elements:

我有这个元素:

Type Posizione
    Name As String
    Position As Byte
End Type
Public Location() as Posizioni

I'd like to loop between the minimum posizione().valore and the max one.

我想在最小的posizione()。valore和最大的一个之间循环。

I tried with:

我尝试过:

For i = LBound(Location().position) To UBound(Location().position)

But I receive "Invalid Qualifier" in this position.

但是我在这个位置收到“无效资格赛”。

Any suggestions?

2 个解决方案

#1


2  

Your array is the variable Location, so you have to use this as parameter to lbound and ubound

您的数组是变量Location,因此您必须将此作为lbound和ubound的参数

For i = LBound(Location) To UBound(Location)
    Location(i).position = i
    Location(i).Name = "Hello " & i
Next i

#2


1  

You will have to find the min and max values before iterating between them. Something like this will do that:

在迭代它们之前,您必须找到最小值和最大值。像这样的东西会这样做:

Dim PosMin As Byte
Dim PosMax As Byte

PosMin = 255
PosMax = 0
For i = LBound(Location) To UBound(Location)
    If Location(i).Position > PosMax Then
        PosMax = Location(i).Position
    End If
    If Location(i).Position < PosMin Then
        PosMin = Location(i).Position
    End If
Next
For i = PosMin To PosMax
    Debug.Print i
Next i

#1


2  

Your array is the variable Location, so you have to use this as parameter to lbound and ubound

您的数组是变量Location,因此您必须将此作为lbound和ubound的参数

For i = LBound(Location) To UBound(Location)
    Location(i).position = i
    Location(i).Name = "Hello " & i
Next i

#2


1  

You will have to find the min and max values before iterating between them. Something like this will do that:

在迭代它们之前,您必须找到最小值和最大值。像这样的东西会这样做:

Dim PosMin As Byte
Dim PosMax As Byte

PosMin = 255
PosMax = 0
For i = LBound(Location) To UBound(Location)
    If Location(i).Position > PosMax Then
        PosMax = Location(i).Position
    End If
    If Location(i).Position < PosMin Then
        PosMin = Location(i).Position
    End If
Next
For i = PosMin To PosMax
    Debug.Print i
Next i