Results 1 to 12 of 12

Thread: Refer to Excel collections in VBA with a "variable" name?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,718
    Location
    I'd use a Dictionary as the main 'container' with a class containing a collection as it's elements

    I just think it's a little more flebible than a Collection

    This needs error checking and the class could probably have more functionality included, but might give you some ideas


    Standard module -------------------

    Option Explicit
    
    
    Sub Example()
        Dim KeyWords As Object
        Dim KeyWordsList As clsKeyWords
        Dim i As Long
        
        Set KeyWords = CreateObject("Scripting.Dictionary")
        Set KeyWordsList = New clsKeyWords
        
        With KeyWords
            Set KeyWordsList = New clsKeyWords
            .Add "KeyWordsBank1List", KeyWordsList
            Set KeyWordsList = New clsKeyWords
            .Add "KeyWordsBank2List", KeyWordsList
            Set KeyWordsList = New clsKeyWords
            .Add "KeyWordsBank3List", KeyWordsList
        End With
        
        i = 1
        With KeyWords("KeyWordsBank" & i & "List")
            .Entry("Key001") = "Data001"
            .Entry("Key002") = "Data002"
            .Entry("Key003") = "Data003"
        End With
                
        i = 3
        With KeyWords("KeyWordsBank" & i & "List")
            .Entry("Key001") = "Data004"
            .Entry("Key002") = "Data005"
            .Entry("Key003") = "Data006"
        End With
    
    
        
        MsgBox KeyWords("KeyWordsBank1List").KeyWords("Key001")
        MsgBox KeyWords("KeyWordsBank3List").Entry("Key001")
    
    
    End Sub

    clsKeyWords module ------------------------------------------

    Option Explicit
    
    Private m_KeyWords As Collection
    
    
    Private Sub Class_Initialize()
        Set m_KeyWords = New Collection
    End Sub
    
    
    Private Sub Class_Terminate()
        Set m_KeyWords = Nothing
    End Sub
    
    
    Property Get KeyWords() As Collection
        Set KeyWords = m_KeyWords
    End Property
    
    
    Property Let Entry(K As String, V As Variant)
        m_KeyWords.Add V, K
    End Property
    
    
    Property Get Entry(K As String) As Variant
        Entry = m_KeyWords(K)
    End Property
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •