+ Reply to Thread
Results 1 to 6 of 6

More Listbox help..

  1. #1
    Registered User
    Join Date
    04-29-2004
    Posts
    92

    More Listbox help..

    I'm wondering how I can Check for duplicates in a listbox. The listbox is filled with values from a combobox and has numbers 1000 -8000. I need to say ok, You've already entered a value for 1000 please check the posting code and try again.


    I was trying something like

    Please Login or Register  to view this content.
    Its a little more complicated then that but this is just a relative idea of what im trying to do.

    Thanks in advance...
    Chris

  2. #2
    Bob Phillips
    Guest

    Re: More Listbox help..

    You either have to loop through every item checking to see whether it is
    used in the combo, or if the combo is linked to a worksheet range, you could
    use MATCH to check it

    If Not Iserror(application.match(userform2.listbox1.value , range("A1:A20"))
    then
    msgbox "im sorry you've entered duplicate charges"
    etc.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "justchris" <[email protected]> wrote
    in message news:[email protected]...
    >
    > I'm wondering how I can Check for duplicates in a listbox. The listbox
    > is filled with values from a combobox and has numbers 1000 -8000. I
    > need to say ok, You've already entered a value for 1000 please check
    > the posting code and try again.
    >
    >
    > I was trying something like
    >
    >
    > Code:
    > --------------------
    > if userform2.listbox1.value = userform2.combobox1.value then
    > msgbox "im sorry you've entered duplicate charges"
    > else
    > with userform2.listbox1
    > .additem userform2.combobox1.value
    > end with
    > --------------------
    >
    >
    > Its a little more complicated then that but this is just a relative
    > idea of what im trying to do.
    >
    > Thanks in advance...
    > Chris
    >
    >
    > --
    > justchris
    > ------------------------------------------------------------------------
    > justchris's Profile:

    http://www.excelforum.com/member.php...fo&userid=8894
    > View this thread: http://www.excelforum.com/showthread...hreadid=401393
    >




  3. #3
    Patrick Molloy
    Guest

    RE: More Listbox help..

    I like to use a scriting dictionayr for this. set a refetence to MS scripting
    runtime.
    A dictionary is like a collection, but has more features and especially an
    Exists method that is useful for checking if a 'key'already exists.

    so
    DIM dic as Scripting.Dictionary

    SET dic = New Scripting.Dictionary

    loading the listbox...

    for eact item in loadlist
    IF NOT dic.Exists(item) THEN
    dic.Add item, item
    listbox1.Add item
    END IF
    Next

    the same check can be used to check when a user tries to ad an item...

    IF dic.Exists(item) THEN
    msgbox "This item already exists in the list",,item
    ELSE
    dic.Add item, item
    listbox1.Add item
    END IF

    HTH






    "justchris" wrote:

    >
    > I'm wondering how I can Check for duplicates in a listbox. The listbox
    > is filled with values from a combobox and has numbers 1000 -8000. I
    > need to say ok, You've already entered a value for 1000 please check
    > the posting code and try again.
    >
    >
    > I was trying something like
    >
    >
    > Code:
    > --------------------
    > if userform2.listbox1.value = userform2.combobox1.value then
    > msgbox "im sorry you've entered duplicate charges"
    > else
    > with userform2.listbox1
    > .additem userform2.combobox1.value
    > end with
    > --------------------
    >
    >
    > Its a little more complicated then that but this is just a relative
    > idea of what im trying to do.
    >
    > Thanks in advance...
    > Chris
    >
    >
    > --
    > justchris
    > ------------------------------------------------------------------------
    > justchris's Profile: http://www.excelforum.com/member.php...fo&userid=8894
    > View this thread: http://www.excelforum.com/showthread...hreadid=401393
    >
    >


  4. #4
    Bob Phillips
    Guest

    Re: More Listbox help..

    Patrick,

    An IfExists function for a collection


    '--------------------------------------------------------------------------
    Public Function ExistsInCollection(col As Collection, ByVal sKey As String)
    '--------------------------------------------------------------------------
    On Error Goto NoSuchKey
    If VarType(col.Item(sKey)) = vbObject Then
    ' force an error condition if key does not exist
    End If
    ExistsInCollection = True
    Exit Function

    NoSuchKey:
    ExistsInCollection = False
    End Function



    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Patrick Molloy" <[email protected]> wrote in message
    news:[email protected]...
    > I like to use a scriting dictionayr for this. set a refetence to MS

    scripting
    > runtime.
    > A dictionary is like a collection, but has more features and especially an
    > Exists method that is useful for checking if a 'key'already exists.
    >
    > so
    > DIM dic as Scripting.Dictionary
    >
    > SET dic = New Scripting.Dictionary
    >
    > loading the listbox...
    >
    > for eact item in loadlist
    > IF NOT dic.Exists(item) THEN
    > dic.Add item, item
    > listbox1.Add item
    > END IF
    > Next
    >
    > the same check can be used to check when a user tries to ad an item...
    >
    > IF dic.Exists(item) THEN
    > msgbox "This item already exists in the list",,item
    > ELSE
    > dic.Add item, item
    > listbox1.Add item
    > END IF
    >
    > HTH
    >
    >
    >
    >
    >
    >
    > "justchris" wrote:
    >
    > >
    > > I'm wondering how I can Check for duplicates in a listbox. The listbox
    > > is filled with values from a combobox and has numbers 1000 -8000. I
    > > need to say ok, You've already entered a value for 1000 please check
    > > the posting code and try again.
    > >
    > >
    > > I was trying something like
    > >
    > >
    > > Code:
    > > --------------------
    > > if userform2.listbox1.value = userform2.combobox1.value then
    > > msgbox "im sorry you've entered duplicate charges"
    > > else
    > > with userform2.listbox1
    > > .additem userform2.combobox1.value
    > > end with
    > > --------------------
    > >
    > >
    > > Its a little more complicated then that but this is just a relative
    > > idea of what im trying to do.
    > >
    > > Thanks in advance...
    > > Chris
    > >
    > >
    > > --
    > > justchris
    > > ------------------------------------------------------------------------
    > > justchris's Profile:

    http://www.excelforum.com/member.php...fo&userid=8894
    > > View this thread:

    http://www.excelforum.com/showthread...hreadid=401393
    > >
    > >




  5. #5
    Patrick Molloy
    Guest

    Re: More Listbox help..

    great. thanks Bob

    "Bob Phillips" wrote:

    > Patrick,
    >
    > An IfExists function for a collection
    >
    >
    > '--------------------------------------------------------------------------
    > Public Function ExistsInCollection(col As Collection, ByVal sKey As String)
    > '--------------------------------------------------------------------------
    > On Error Goto NoSuchKey
    > If VarType(col.Item(sKey)) = vbObject Then
    > ' force an error condition if key does not exist
    > End If
    > ExistsInCollection = True
    > Exit Function
    >
    > NoSuchKey:
    > ExistsInCollection = False
    > End Function
    >
    >
    >
    > --
    >
    > HTH
    >
    > RP
    > (remove nothere from the email address if mailing direct)
    >
    >
    > "Patrick Molloy" <[email protected]> wrote in message
    > news:[email protected]...
    > > I like to use a scriting dictionayr for this. set a refetence to MS

    > scripting
    > > runtime.
    > > A dictionary is like a collection, but has more features and especially an
    > > Exists method that is useful for checking if a 'key'already exists.
    > >
    > > so
    > > DIM dic as Scripting.Dictionary
    > >
    > > SET dic = New Scripting.Dictionary
    > >
    > > loading the listbox...
    > >
    > > for eact item in loadlist
    > > IF NOT dic.Exists(item) THEN
    > > dic.Add item, item
    > > listbox1.Add item
    > > END IF
    > > Next
    > >
    > > the same check can be used to check when a user tries to ad an item...
    > >
    > > IF dic.Exists(item) THEN
    > > msgbox "This item already exists in the list",,item
    > > ELSE
    > > dic.Add item, item
    > > listbox1.Add item
    > > END IF
    > >
    > > HTH
    > >
    > >
    > >
    > >
    > >
    > >
    > > "justchris" wrote:
    > >
    > > >
    > > > I'm wondering how I can Check for duplicates in a listbox. The listbox
    > > > is filled with values from a combobox and has numbers 1000 -8000. I
    > > > need to say ok, You've already entered a value for 1000 please check
    > > > the posting code and try again.
    > > >
    > > >
    > > > I was trying something like
    > > >
    > > >
    > > > Code:
    > > > --------------------
    > > > if userform2.listbox1.value = userform2.combobox1.value then
    > > > msgbox "im sorry you've entered duplicate charges"
    > > > else
    > > > with userform2.listbox1
    > > > .additem userform2.combobox1.value
    > > > end with
    > > > --------------------
    > > >
    > > >
    > > > Its a little more complicated then that but this is just a relative
    > > > idea of what im trying to do.
    > > >
    > > > Thanks in advance...
    > > > Chris
    > > >
    > > >
    > > > --
    > > > justchris
    > > > ------------------------------------------------------------------------
    > > > justchris's Profile:

    > http://www.excelforum.com/member.php...fo&userid=8894
    > > > View this thread:

    > http://www.excelforum.com/showthread...hreadid=401393
    > > >
    > > >

    >
    >
    >


  6. #6
    Dave Peterson
    Guest

    Re: More Listbox help..

    A dropped 0?

    If Not Iserror(application.match(userform2.listbox1.value , range("A1:A20"), 0)




    Bob Phillips wrote:
    >
    > You either have to loop through every item checking to see whether it is
    > used in the combo, or if the combo is linked to a worksheet range, you could
    > use MATCH to check it
    >
    > If Not Iserror(application.match(userform2.listbox1.value , range("A1:A20"))
    > then
    > msgbox "im sorry you've entered duplicate charges"
    > etc.
    >
    > --
    >
    > HTH
    >
    > RP
    > (remove nothere from the email address if mailing direct)
    >
    > "justchris" <[email protected]> wrote
    > in message news:[email protected]...
    > >
    > > I'm wondering how I can Check for duplicates in a listbox. The listbox
    > > is filled with values from a combobox and has numbers 1000 -8000. I
    > > need to say ok, You've already entered a value for 1000 please check
    > > the posting code and try again.
    > >
    > >
    > > I was trying something like
    > >
    > >
    > > Code:
    > > --------------------
    > > if userform2.listbox1.value = userform2.combobox1.value then
    > > msgbox "im sorry you've entered duplicate charges"
    > > else
    > > with userform2.listbox1
    > > .additem userform2.combobox1.value
    > > end with
    > > --------------------
    > >
    > >
    > > Its a little more complicated then that but this is just a relative
    > > idea of what im trying to do.
    > >
    > > Thanks in advance...
    > > Chris
    > >
    > >
    > > --
    > > justchris
    > > ------------------------------------------------------------------------
    > > justchris's Profile:

    > http://www.excelforum.com/member.php...fo&userid=8894
    > > View this thread: http://www.excelforum.com/showthread...hreadid=401393
    > >


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1