PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average.

Combobox.additem (No Repeats)

 
 
PaulW
Guest
Posts: n/a
 
      9th Dec 2007
Hi there,

I'm building a Userform with a bunch of Comboboxes which I am populating
from a spreadsheet.

One of the Combobox is beingin populated by a field which has the majority
of the entries the same.

Can anyone help with some code to only populate the Combobox with uniqie
entries only.

Code I have been using to populate the Combobox has been:

sub unserform_initialize ()
a = 2
do until cells(a,1)=""
combobox1.additem cells(a,24)
a = a+1
loop
end sub

Thanks
Dan
 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      9th Dec 2007
Private Sub Userform_Initialize()
Dim ary
Dim idx As Long
Dim rownum As Long

ReDim ary(1 To 1)
ary(1) = Cells(1, "A").Value

idx = 1
rownum = 2
Do Until Cells(rownum, "A") = ""
If UBound(Filter(ary, Cells(rownum, "A"))) = -1 Then
idx = idx + 1
ReDim Preserve ary(1 To idx)
ary(idx) = Cells(rownum, "A")
End If
rownum = rownum + 1
Loop
ComboBox1.List = ary
End Sub


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"PaulW" <(E-Mail Removed)> wrote in message
news:70A90A00-0DC9-4968-8A96-(E-Mail Removed)...
> Hi there,
>
> I'm building a Userform with a bunch of Comboboxes which I am populating
> from a spreadsheet.
>
> One of the Combobox is beingin populated by a field which has the majority
> of the entries the same.
>
> Can anyone help with some code to only populate the Combobox with uniqie
> entries only.
>
> Code I have been using to populate the Combobox has been:
>
> sub unserform_initialize ()
> a = 2
> do until cells(a,1)=""
> combobox1.additem cells(a,24)
> a = a+1
> loop
> end sub
>
> Thanks
> Dan



 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      9th Dec 2007
the code below check if item is in combobox before adding new item

Sub unserform_initialize()
a = 2
ActiveSheet.ComboBox1.Clear
Do Until Cells(a, 24) = ""
Found = False
For i = 0 To (ActiveSheet.ComboBox1.ListCount - 1)
If ActiveSheet.ComboBox1.List(i) = _
Format(Cells(a, 24), Text) Then
Found = True
Exit For
End If
Next i
If Found = False Then
ActiveSheet.ComboBox1.AddItem Cells(a, 24)
End If
a = a + 1
Loop
End Sub

"PaulW" wrote:

> Hi there,
>
> I'm building a Userform with a bunch of Comboboxes which I am populating
> from a spreadsheet.
>
> One of the Combobox is beingin populated by a field which has the majority
> of the entries the same.
>
> Can anyone help with some code to only populate the Combobox with uniqie
> entries only.
>
> Code I have been using to populate the Combobox has been:
>
> sub unserform_initialize ()
> a = 2
> do until cells(a,1)=""
> combobox1.additem cells(a,24)
> a = a+1
> loop
> end sub
>
> Thanks
> Dan

 
Reply With Quote
 
PaulW
Guest
Posts: n/a
 
      9th Dec 2007
Excellent thanks guys, once again you help me out of a tight spot.

"Joel" wrote:

> the code below check if item is in combobox before adding new item
>
> Sub unserform_initialize()
> a = 2
> ActiveSheet.ComboBox1.Clear
> Do Until Cells(a, 24) = ""
> Found = False
> For i = 0 To (ActiveSheet.ComboBox1.ListCount - 1)
> If ActiveSheet.ComboBox1.List(i) = _
> Format(Cells(a, 24), Text) Then
> Found = True
> Exit For
> End If
> Next i
> If Found = False Then
> ActiveSheet.ComboBox1.AddItem Cells(a, 24)
> End If
> a = a + 1
> Loop
> End Sub
>
> "PaulW" wrote:
>
> > Hi there,
> >
> > I'm building a Userform with a bunch of Comboboxes which I am populating
> > from a spreadsheet.
> >
> > One of the Combobox is beingin populated by a field which has the majority
> > of the entries the same.
> >
> > Can anyone help with some code to only populate the Combobox with uniqie
> > entries only.
> >
> > Code I have been using to populate the Combobox has been:
> >
> > sub unserform_initialize ()
> > a = 2
> > do until cells(a,1)=""
> > combobox1.additem cells(a,24)
> > a = a+1
> > loop
> > end sub
> >
> > Thanks
> > Dan

 
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
additem to combobox with an array jocke Microsoft Excel Misc 2 29th Sep 2005 07:56 PM
Using AddItem On Combobox and list box Roy Goldhammer Microsoft Access Form Coding 1 5th Aug 2005 09:47 PM
combobox additem action =?Utf-8?B?SmFjaw==?= Microsoft Access VBA Modules 3 9th Apr 2005 10:51 PM
combobox additem masterphilch Microsoft Excel Programming 2 25th Oct 2004 11:04 PM
RE: AddItem Method - Combobox value Todd Huttenstine Microsoft Excel Programming 2 21st Apr 2004 05:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:39 AM.