2013-10-27 104 views
3

阿霍伊HOY,按名称获取VBA集合项目

我试图通过引用它做的东西到自定义对象的自定义集合在VBA Excel中的name属性。我发誓它之前(或至少没有发生错误),现在它的工作。当我尝试使用字符串尝试Get时,出现invalid call or argument错误。提前致谢,甚至阅读此,任何帮助表示赞赏。 < \编辑>

这里的集合:

Option Explicit 

Private DRAFields As New Collection 

Sub Add(Name As String, Optional colNbr As Long, Optional Exists As Boolean) 
    Dim fld As New DRAFld 
    fld.colNbr = colNbr 
    fld.Name = Name 
    fld.Exists = Exists 

    DRAFields.Add fld 
End Sub 

Property Get Item(NameOrNumber As Variant) 
    Set Item = DRAFields(NameOrNumber) '<------- Error here 
End Property 

馆藏有通过传递名称的阵列中的一个函数添加的项目,并没有问题,返回集合。我可以通过使用密钥进行迭代。以防万一Debug.Print myFlds.Item("Customer").colNbr

和对象类:

Option Explicit 

Private clmNbrPvt  As Long 
Private namePvt   As String 
Private existsPvt  As Boolean 

Public Property Get colNbr() As Long 
    colNbr = clmNbrPvt 
End Property 
Public Property Let colNbr(lngParam As Long) 
    clmNbrPvt = lngParam 
End Property 


Public Property Get Name() As String 
    Name = namePvt 
End Property 

Public Property Let Name(strParam As String) 
    namePvt = strParam 
End Property 


Public Property Get Exists() As Boolean 
    Exists = existsPvt 
End Property 
Public Property Let Exists(booParam As Boolean) 
    existsPvt = booParam 
End Property 

为什么不说功能太:但是如果让这样的错误发生

Function validateAndBuildDRAFields(ByRef arrReqFields() As String, _ 
    inputSheet As Worksheet, _ 
    Optional VBAModule As String) As clsDRAFields 

Dim lEndCol  As Long: lEndCol = Standard.zGetLastColumn(inputSheet, 1) 
Dim i   As Long 
Dim x   As Long 
Dim intExit  As Long 
Dim myDRAFields As New clsDRAFields 

    Set validateAndBuildDRAFields = myDRAFields 

    'Builds myDRAFields items from arrReqFields 
    For i = LBound(arrReqFields) To UBound(arrReqFields) 
     myDRAFields.Add arrReqFields(i) 
    Next i 

    'checks if required fields exist on input sheet 
    'if found then sets column number and exists = true 
    For i = 1 To myDRAFields.Count 
     For x = 1 To lEndCol 
      If inputSheet.Cells(1, x) = myDRAFields.Item(i).Name Then 
       myDRAFields.Item(i).colNbr = x 
       myDRAFields.Item(i).Exists = True 
       intExit = intExit + 1 
       Exit For 
      End If 
     Next x 
     If intExit = UBound(arrReqFields) + 1 Then Exit For 
    Next i 

    ' tells user if there are any missing fields and ends if true 
    If (Not intExit = UBound(arrReqFields) + 1) Or _ 
     intExit = 0 Then 
     For i = 1 To myDRAFields.Count 
      If myDRAFields.Item(i).Exists = False Then 
       Call Standard.TheEndWithError("I couldn't find the " & myDRAFields.Item(i).Name & _ 
        " column in your file. Please add " & myDRAFields.Item(i).Name & _ 
        " to your DRA Layout.", False, VBAModule) 
      End If 
     Next i 
     Set myDRAFields = Nothing 
     Standard.TheEnd 
    End If 
End Function 

回答

7

通过访问集合项目的键,当您将项目添加到集合时,您必须提供一个键。关键是可选的。当您使用字符串访问集合项目时,Item方法假定您希望匹配密钥。当你使用一个整数时,它假定你想要位置索引。

所以,在你的Add方法将该行更改为

DRAFields.Add fld, fld.Name 

,你就可以通过他们的名称属性来访问项目。

+0

哇,这是我在查找如何使用集合时错过的重要一点。非常感谢! – Bippy