Consulting

Results 1 to 4 of 4

Thread: Test if an item exists within a collection data type

  1. #1
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location

    Test if an item exists within a collection data type

    Guys,

    I'm doing a little test using the item method. My sample data in column A contains a lot of repeating values. My code below loads unique values to a collection. This part is working OK. My code errors out with a message "Run-time Error '424' Object required". What am I missing?

    Dim cUnique As Collection
        Dim Rng As Range
        Dim Cel As Range
        Dim sh As Worksheet
        Dim N As Variant, fvalue
    Set sh = ThisWorkbook.Sheets("Sheet1")
        Set Rng = sh.Range("A1", sh.Range("A1").End(xlDown))
        Set cUnique = New Collection
    On Error Resume Next
            For Each Cel In Rng.Cells
                cUnique.Add Cel.Value, CStr(Cel.Value)
            Next Cel
        On Error GoTo 0
    fvalue = cUnique.Item("Lemon")
    If Error.Number = 0 Then
    msgbox "found"
    Else
    msgbox "not found"
    End If


    Thanks & regards


    kp
    Last edited by Aussiebear; 04-19-2023 at 04:00 PM. Reason: Adjusted the code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sub Test()
    Dim cUnique As Collection
    Dim Rng As Range
    Dim Cel As Range
    Dim sh As Worksheet
    Dim N As Variant, fvalue
    Set sh = ThisWorkbook.Sheets("Sheet1")
        Set Rng = sh.Range("A1", sh.Range("A1").End(xlDown))
        Set cUnique = New Collection
    On Error Resume Next
        For Each Cel In Rng.Cells
            cUnique.Add Cel.Value, CStr(Cel.Value)
        Next Cel
        On Error GoTo 0
    If ExistsInCollection(cUnique, "Lemon") Then
    MsgBox "found"
        Else
    MsgBox "not found"
        End If
    End Sub
    
    Public Function ExistsInCollection(pColl, ByVal pKey As String) As Boolean
        On Error GoTo NoSuchKey
        If VarType(pColl.Item(pKey)) = vbObject Then
             ' force an error condition if key does not exist
        End If
        ExistsInCollection = True
        Exit Function
    NoSuchKey:
        ExistsInCollection = False
    End Function
    Last edited by Aussiebear; 04-19-2023 at 04:01 PM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Dim cUnique As Collection
    Dim Rng As Range
    Dim Cel As Range
    Dim sh As Worksheet
    Dim N As Variant, fvalue
    Set sh = ThisWorkbook.Sheets("Sheet1")
    Set Rng = sh.Range("A1", sh.Range("A1").End(xlDown))
    Set cUnique = New Collection
    On Error Resume Next
    For Each Cel In Rng.Cells
        cUnique.Add Cel.Value, CStr(Cel.Value)
    Next Cel
    On Error GoTo jump
    fvalue = cUnique.Item("Lemon")
    MsgBox "found"
    Exit Sub
    jump:
    MsgBox "not found"
    Last edited by Aussiebear; 04-19-2023 at 04:01 PM. Reason: Adjusted the code tags
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  4. #4
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Brilliant. Both solutions work beautifully.

    Thanks Bob & Georgiboy.


    kp

Posting Permissions

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