VBA Excel – 使用Get / Let属性将string数组传递给类变体types

我一直在寻找一个答案只是传递和返回一个string数组在VB模块。 以下是我的示例代码。 我不断收到错误“不能分配给arrays”上线

Orgs.pIDOrgList = ID 

有什么想法吗?

class级代码:

 'Class COrgList Private m_vpIDOrgList() As Variant 'Public pGROrgList() As String Function getOrgList(Comp As String) As String() If Comp = "Gram Stain" Then getOrgList = m_pGROrgList ElseIf Comp = "Identification" Then getOrgList = m_pIDOrgList Else MsgBox "Incorrect Comp Name" End If End Function Public Property Get pIDOrgList() As Variant() pIDOrgList = m_vpIDOrgList() End Property Public Property Let pIDOrgList(vpIDOrgList() As Variant) m_vpIDOrgList = vpIDOrgList End Property 

模块testing代码:

 Sub test() Dim Orgs As COrgList Set Orgs = New COrgList Dim ID(2) As String Dim GR(2) As String ID(0) = "0" ID(1) = "2" ID(2) = "1" Debug.Print ID(0) Orgs.pIDOrgList = ID Debug.Print Orgs.getOrgList("Identifciation")(1) End Sub 

谢谢!

您不能将一个数组分配给另一个数组,但您可以将一个数组分配给一个简单的variables。( http://msdn.microsoft.com/zh-cn/library/office/gg264711 ( v=office.14).aspx或http://www.cpearson.com/excel/passingandreturningarrays.htm

所以应该是:

 'Class COrgList Private m_vpIDOrgList As Variant 'Public pGROrgList() As String Function getOrgList(Comp As String) As String() If Comp = "Gram Stain" Then 'getOrgList = m_pGROrgList ElseIf Comp = "Identification" Then getOrgList = m_vpIDOrgList Else MsgBox "Incorrect Comp Name" End If End Function Public Property Get pIDOrgList() As Variant pIDOrgList = m_vpIDOrgList End Property Public Property Let pIDOrgList(vpIDOrgList As Variant) m_vpIDOrgList = vpIDOrgList End Property 

Debug.Print Orgs.getOrgList(“Identifciation”)(1)中的拼写错误,