PC Review


Reply
Thread Tools Rate Thread

check if values is already in listbox

 
 
=?Utf-8?B?RnJhbmsgTQ==?=
Guest
Posts: n/a
 
      16th Nov 2007
hello,

i'm working with excel 2000 and i have the next code in my program:

Private Sub cmb1_Click()
myRow = ActiveCell.Row
Dim Bcell As Range
Form1.ListBox1.Clear
For Each Bcell In Range(("A" & myRow) & (":J" & myRow))
If Bcell.Value = "C" Or Bcell.Value = "S" Or Bcell.Value = "I" Then
Form1.ListBox1.AddItem
ThisWorkbook.Sheets("Sheet1").Range(((ConvertToLetter(Bcell.Column)) & "1"))
End If
Next Bcell
end sub

so basicly it does the following when myrow = 4
it searches for the values C, S or I in the range A4:J4. When one of these
letters is found the value of row 1 in that same column is added into the
listbox. But now i want a check that makes sure that if the value of row 1 is
already in the listbox, it is not added again. I know the code has to come
after If Bcell.Value = "C" Or ... and that it has to loop through the
listbox, but i have no idea what the code has to be. Can any help me with it?


 
Reply With Quote
 
 
 
 
paul.robinson@it-tallaght.ie
Guest
Posts: n/a
 
      16th Nov 2007
On Nov 16, 10:39 am, Frank M <Fra...@discussions.microsoft.com> wrote:
> hello,
>
> i'm working with excel 2000 and i have the next code in my program:
>
> Private Sub cmb1_Click()
> myRow = ActiveCell.Row
> Dim Bcell As Range
> Form1.ListBox1.Clear
> For Each Bcell In Range(("A" & myRow) & (":J" & myRow))
> If Bcell.Value = "C" Or Bcell.Value = "S" Or Bcell.Value = "I" Then
> Form1.ListBox1.AddItem
> ThisWorkbook.Sheets("Sheet1").Range(((ConvertToLetter(Bcell.Column)) & "1"))
> End If
> Next Bcell
> end sub
>
> so basicly it does the following when myrow = 4
> it searches for the values C, S or I in the range A4:J4. When one of these
> letters is found the value of row 1 in that same column is added into the
> listbox. But now i want a check that makes sure that if the value of row 1 is
> already in the listbox, it is not added again. I know the code has to come
> after If Bcell.Value = "C" Or ... and that it has to loop through the
> listbox, but i have no idea what the code has to be. Can any help me with it?


Hi
This is a classic problem we all come across eventually.
Add the items to a collection first, then add those to the list

Private Sub cmb1_Click()
myRow = ActiveCell.Row
Dim Bcell As Range
Dim ListCollection as New Collection, ListValue as Variant, item as
Variant
Form1.ListBox1.Clear
On error resume next
For Each Bcell In Range(("A" & myRow) & (":J" & myRow))
If Bcell.Value = "C" Or Bcell.Value = "S" Or Bcell.Value = "I"
Then
ListValue =
ThisWorkbook.Sheets("Sheet1").Range(((ConvertToLetter(Bcell.Column)) &
"1"))
ListCollection.Add ListValue, Cstr(ListValue)
End if
Next Bcell
on error goto 0
'now add to listbox
for each Item in ListCollection
Form1.ListBox1.AddItem Item
next item
end sub

The add in a collection creates an error if the item already exists,
and the copy is not added. The "on error" bits catch the errors.
Check John Walkenbach's website for more on this (like sorting the
list) - I think this was his idea first.

regards
Paul
 
Reply With Quote
 
=?Utf-8?B?RnJhbmsgTQ==?=
Guest
Posts: n/a
 
      16th Nov 2007
Thanks, this works exactly how i want it

"(E-Mail Removed)" wrote:

> On Nov 16, 10:39 am, Frank M <Fra...@discussions.microsoft.com> wrote:
> > hello,
> >
> > i'm working with excel 2000 and i have the next code in my program:
> >
> > Private Sub cmb1_Click()
> > myRow = ActiveCell.Row
> > Dim Bcell As Range
> > Form1.ListBox1.Clear
> > For Each Bcell In Range(("A" & myRow) & (":J" & myRow))
> > If Bcell.Value = "C" Or Bcell.Value = "S" Or Bcell.Value = "I" Then
> > Form1.ListBox1.AddItem
> > ThisWorkbook.Sheets("Sheet1").Range(((ConvertToLetter(Bcell.Column)) & "1"))
> > End If
> > Next Bcell
> > end sub
> >
> > so basicly it does the following when myrow = 4
> > it searches for the values C, S or I in the range A4:J4. When one of these
> > letters is found the value of row 1 in that same column is added into the
> > listbox. But now i want a check that makes sure that if the value of row 1 is
> > already in the listbox, it is not added again. I know the code has to come
> > after If Bcell.Value = "C" Or ... and that it has to loop through the
> > listbox, but i have no idea what the code has to be. Can any help me with it?

>
> Hi
> This is a classic problem we all come across eventually.
> Add the items to a collection first, then add those to the list
>
> Private Sub cmb1_Click()
> myRow = ActiveCell.Row
> Dim Bcell As Range
> Dim ListCollection as New Collection, ListValue as Variant, item as
> Variant
> Form1.ListBox1.Clear
> On error resume next
> For Each Bcell In Range(("A" & myRow) & (":J" & myRow))
> If Bcell.Value = "C" Or Bcell.Value = "S" Or Bcell.Value = "I"
> Then
> ListValue =
> ThisWorkbook.Sheets("Sheet1").Range(((ConvertToLetter(Bcell.Column)) &
> "1"))
> ListCollection.Add ListValue, Cstr(ListValue)
> End if
> Next Bcell
> on error goto 0
> 'now add to listbox
> for each Item in ListCollection
> Form1.ListBox1.AddItem Item
> next item
> end sub
>
> The add in a collection creates an error if the item already exists,
> and the copy is not added. The "on error" bits catch the errors.
> Check John Walkenbach's website for more on this (like sorting the
> list) - I think this was his idea first.
>
> regards
> Paul
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox: Insert multiple values selected from listbox into databas =?Utf-8?B?QmFudQ==?= Microsoft VB .NET 0 5th Oct 2006 01:56 PM
Excel check box values to Access Check Box values =?Utf-8?B?QkFD?= Microsoft Access Form Coding 1 5th Sep 2006 11:19 PM
ASP.NET Listbox Populated With SQL, Then check Values Dessip Microsoft ASP .NET 1 5th Jul 2006 07:58 PM
Fill values into a listbox matching selected values from a combobox Jon Microsoft Excel Programming 4 25th Jan 2005 04:25 PM
Check Listbox values... Lynn Pennington Microsoft Access VBA Modules 1 14th Nov 2003 02:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:33 AM.