VBA:关于数组自定义类型和函数的更多问题
VBA: More questions about custom Type and Function with arrays
我无法尝试将数组变量(自定义类型)传递给函数。使用 D1
调用函数时出现错误,请问我在这里弄错了什么?
我已经尝试声明 D1()
等,这看起来不正确但无论如何都不起作用。
Public Type CusType
One_ As Integer
Two_ As Single
Thr_ As Single
Fou_ As Single
End Type
Public Sub Test1()
Dim D1 As CusType
Dim D2 As CusType
Dim D3 As CusType
Dim Marker As Integer
Marker = 1
With D1
.One_ = 11
.Two_ = 12
.Thr_ = 13
.Fou_ = 14
End With
With D2
.One_ = 21
.Two_ = 22
.Thr_ = 23
.Fou_ = 24
End With
With D3
.One_ = 31
.Two_ = 32
.Thr_ = 33
.Fou_ = 34
End With
Dim TestResult As CusType
TestResult = Test(Marker, **D1, D2, D3**)
Debug.Print TestResult.One_ & ","; TestResult.Two_ & ","; TestResult.Thr_ & ","; TestResult.Fou_
End Sub
Public Function Test(R, A, B, C) As CusType
Dim First, Second, Third, Fourth As Single
If R = 0 Then
Test = A
ElseIf R = 1 Then
Test = B
Else
Test = C
End If
End Function
报错信息清楚的告诉你:
Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions
(在问题中包含准确的错误消息会很有帮助。)
您没有为 Test
声明参数和 return 类型,因此您的结构将被强制转换为 Variant
。
声明类型:
Public Function Test(ByVal R As Long, A As CusType, B As CusType, C As CusType) As CusType
我无法尝试将数组变量(自定义类型)传递给函数。使用 D1
调用函数时出现错误,请问我在这里弄错了什么?
我已经尝试声明 D1()
等,这看起来不正确但无论如何都不起作用。
Public Type CusType
One_ As Integer
Two_ As Single
Thr_ As Single
Fou_ As Single
End Type
Public Sub Test1()
Dim D1 As CusType
Dim D2 As CusType
Dim D3 As CusType
Dim Marker As Integer
Marker = 1
With D1
.One_ = 11
.Two_ = 12
.Thr_ = 13
.Fou_ = 14
End With
With D2
.One_ = 21
.Two_ = 22
.Thr_ = 23
.Fou_ = 24
End With
With D3
.One_ = 31
.Two_ = 32
.Thr_ = 33
.Fou_ = 34
End With
Dim TestResult As CusType
TestResult = Test(Marker, **D1, D2, D3**)
Debug.Print TestResult.One_ & ","; TestResult.Two_ & ","; TestResult.Thr_ & ","; TestResult.Fou_
End Sub
Public Function Test(R, A, B, C) As CusType
Dim First, Second, Third, Fourth As Single
If R = 0 Then
Test = A
ElseIf R = 1 Then
Test = B
Else
Test = C
End If
End Function
报错信息清楚的告诉你:
Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions
(在问题中包含准确的错误消息会很有帮助。)
您没有为 Test
声明参数和 return 类型,因此您的结构将被强制转换为 Variant
。
声明类型:
Public Function Test(ByVal R As Long, A As CusType, B As CusType, C As CusType) As CusType