VBA:使用自定义类型将变量传递给sub()(VBA: passing variables to sub() with custom type)

编程入门 行业动态 更新时间:2024-10-08 08:29:05
VBA:使用自定义类型将变量传递给sub()(VBA: passing variables to sub() with custom 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

I am playing with calling a sub() but keep getting "User-defined type not defined" error. Unable to figure out after trying different ways of declaring variables as array. Would appreciate any guidance on this:

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语句。

您需要重新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

Your For i = 0 To 5 is missing the closing Next i statement.

You need to Redim the size of your thisis() array:

ReDim thisis(o To 5)

the whole "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

更多推荐

本文发布于:2023-08-04 20:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   变量   类型   VBA   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!