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

问题描述:

我正在玩调用sub(),但不断收到“用户定义类型未定义”错误。尝试使用不同的方法将变量声明为数组后,无法弄清楚。想知道关于这个的任何指导:VBA:将自定义类型的变量传递给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 
+0

一旦我维'thisis'得当,加上'Next'到你的for循环,并改变调用thisis(),athing'到'被调用this,athing',它没有问题。 –

+0

谢谢@Johncoleman。显然不在尺寸之上。或者剪切和粘贴技巧! – user110084

+0

您何时需要实际使用CALL语句?为什么在调用函数时需要使用somefunction(var1,var2,...),但为了sub,省略了括号?他们有没有需要? – user110084

For i = 0 To 5缺少收盘Next i声明。

您需要Redimthisis()数组的大小:

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 
+0

谢谢@ShaiRado。是的,我在复制和粘贴时不知何故跳过了下一行。在删除模块并插入新模块之前,我还遇到过无法识别自定义类型的问题。 我不知道你可以调用一个没有()像被调用(thesies(),athing)的子。我一直在CALL之前。我可以用功能做同样的格式吗? – user110084

+0

@ShaiRodo,Redim var(o to 5),“o”使它保持不变?没有看到记录 – user110084

+0

调用函数可以是不同的,特别是如果函数返回一个参数,Redim Preserve是一种不同于Redim的方法 –