简体   繁体   中英

vba: loop through user-defined data type

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.

I tried with:

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

But I receive "Invalid Qualifier" in this position.

Any suggestions?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM