VBA:使用自定义类型

时间:2016-09-17 14:39:41

标签: arrays vba parameter-passing subroutine custom-data-type

我正在玩一个sub()但仍然收到“用户定义的类型未定义”错误。在尝试将变量声明为数组的不同方法后,无法弄清楚。将不胜感激任何指导:

Public Type Whatever
    ppp As String
    qqq As Long
    rrr As Single
End Type




Sub isthisworking()
 Dim thisis() As Whatever
 Dim i As Long
 Dim athing As Long

 For i = 0 To 5
    With thisis(i)
        .ppp = i & "p"
        .qqq = i * 2
        .rrr = i ^ 3
    End With

 athing = 20

 beingcalled thisis(), athing

End Sub



Public Sub beingcalled(ByRef thisis() As Whatever, athing As Long)

 Dim cycles As Long

 cycles = UBound(thisis)

 For i = 0 To cycles - 1
    With thisis(i)
        Debug.Print i & ": " & .ppp & "," & .qqq & "," & .rrr
    End With
 Next


End Sub

1 个答案:

答案 0 :(得分:1)

您的For i = 0 To 5缺少结束Next i声明。

您需要Redim thisis()数组的大小:

ReDim thisis(o To 5)

整个“ isthisworking ”Sub:

Sub isthisworking()

Dim thisis() As Whatever
Dim i As Long
Dim athing As Long

ReDim thisis(o To 5)

For i = 0 To 5
   With thisis(i)
       .ppp = i & "p"
       .qqq = i * 2
       .rrr = i ^ 3
   End With
Next i

athing = 20

beingcalled thisis(), athing    
' you can pass also thisis (without the brackets) gives the same result

End Sub