2013-10-27 5 views
3

Ahoy hoy,VBA 컬렉션의 항목 가져 오기

VBA Excel에서 이름 속성을 참조하여 사용자 지정 컬렉션의 사용자 지정 개체를 처리하려고합니다. 나는 그것이 이전에 (또는 적어도 오류를 던지지 않았던) 지금은 그 kaput에서 일했다고 맹세한다. 내가 문자열을 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 

소장품 함수에 이름의 배열을 통과하여 추가 항목이 상기 수집 문제없이 리턴한다 : 여기>

< \ 편집 컬렉션이다. 키를 사용하여 반복 할 수 있습니다. 그것도 작동하지

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 

왜 : 단지의 경우 Debug.Print myFlds.Item("Customer").colNbr

그리고 객체 클래스를 : 같은 얻을하지만 오류가 발생

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 메서드는 키를 일치 시키려한다고 가정합니다. 정수를 사용할 때 위치 인덱스를 원한다고 가정합니다.

그래서,

DRAFields.Add fld, fld.Name 

에 추가하는 방법에 라인을 변경하고 자신의 이름 속성에 의해 항목에 액세스 할 수 있습니다.

+0

와우, 그것은 내가 컬렉션을 사용하는 방법을 찾을 때 놓친 중요한 비트였습니다. 많이 고마워! – Bippy

관련 문제