2016-09-17 71 views
0

我正在玩調用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

回答

1

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的方法 –

相關問題