在VBA Collection by Name中获取项目

时间:2013-10-27 00:48:06

标签: excel vba excel-vba collections

啊,霍伊,

我正在尝试通过在VBA Excel中引用它的名称属性来对自定义集合中的自定义对象执行操作。我发誓它以前工作过(或者至少没有抛出错误)现在它的kaput。当我尝试通过字符串invalid call or argument时,我收到Get错误。也提前感谢您阅读本文,任何帮助表示赞赏。 < \编辑>

这是集合:

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

1 个答案:

答案 0 :(得分:8)

要通过其键访问集合项,您必须在将项添加到集合时提供密钥。钥匙是可选的。当您使用字符串访问集合项时,Item方法假定您要匹配该键。当您使用整数时,它假定您需要位置索引。

因此,将Add方法中的行更改为

DRAFields.Add fld, fld.Name

您将能够通过其Name属性访问项目。