VBA:将变量传递给自定义类型的 sub()

VBA: passing variables to sub() with custom type

我正在尝试调用 sub() 但一直收到 "User-defined type not defined" 错误。在尝试将变量声明为数组的不同方法后无法弄清楚。非常感谢对此的任何指导:

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

您的 For i = 0 To 5 缺少结束语 Next i

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

ReDim thisis(o To 5)

整个“isthisworking”子:

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