PC Review


Reply
Thread Tools Rate Thread

ComboBox Drop Down List

 
 
Johnny
Guest
Posts: n/a
 
      9th Jun 2009
I have 14 Combo Boxes on my Excel form that I want to populate with "Yes" and
"No" with the initialize event. The only way I know how to do it is to use
the following code for each ComboBox.

With ComboBox#
.AddItem "Yes"
.AddItem "No"
End With

Is there simpler code to accomplish this?

Thank you
 
Reply With Quote
 
 
 
 
JLatham
Guest
Posts: n/a
 
      9th Jun 2009
You can set up a pair of cells in a column in your workbook and give them a
name such as YNList and then your code can read:

Me.ComboBox#.RowSource="YNList"

(of course you can skip the Me. if you want).

The named list can be on any sheet in the workbook, even a hidden sheet.

"Johnny" wrote:

> I have 14 Combo Boxes on my Excel form that I want to populate with "Yes" and
> "No" with the initialize event. The only way I know how to do it is to use
> the following code for each ComboBox.
>
> With ComboBox#
> .AddItem "Yes"
> .AddItem "No"
> End With
>
> Is there simpler code to accomplish this?
>
> Thank you

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      9th Jun 2009
If your comboboxes are named nicely:

Dim iCtr As Long
For iCtr = 1 To 14
With Me.Controls("Combobox" & iCtr)
.AddItem "Yes"
.AddItem "No"
End With
Next iCtr



Johnny wrote:
>
> I have 14 Combo Boxes on my Excel form that I want to populate with "Yes" and
> "No" with the initialize event. The only way I know how to do it is to use
> the following code for each ComboBox.
>
> With ComboBox#
> .AddItem "Yes"
> .AddItem "No"
> End With
>
> Is there simpler code to accomplish this?
>
> Thank you


--

Dave Peterson
 
Reply With Quote
 
john
Guest
Posts: n/a
 
      9th Jun 2009
Another way perhaps.

Private Sub UserForm_Initialize()
Dim ctl As Control
Dim CtrlType As String

For Each ctl In UserForm1.Controls

CtrlType = TypeName(ctl)

If CtrlType = "ComboBox" Then

ctl.AddItem "Yes"
ctl.AddItem "No"
ctl.ListIndex = 0

End If
Next ctl
End Sub
--
jb


"Johnny" wrote:

> I have 14 Combo Boxes on my Excel form that I want to populate with "Yes" and
> "No" with the initialize event. The only way I know how to do it is to use
> the following code for each ComboBox.
>
> With ComboBox#
> .AddItem "Yes"
> .AddItem "No"
> End With
>
> Is there simpler code to accomplish this?
>
> Thank you

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      9th Jun 2009
'A one time loop will do

'if you are using a user form...
Dim intTemp As Integer
For intTemp = 1 To 14
Me.Controls("Combobox" & intTemp).AddItem "Yes"
Me.Controls("Combobox" & intTemp).AddItem "No"
Next

'If the comboboxes are in Activesheet
Dim intTemp As Integer
For intTemp = 1 To 14
ActiveSheet.OLEObjects("Combobox" & intTemp).Object.AddItem "Yes"
ActiveSheet.OLEObjects("Combobox" & intTemp).Object.AddItem "No"
Next

If this post helps click Yes
---------------
Jacob Skaria


"Johnny" wrote:

> I have 14 Combo Boxes on my Excel form that I want to populate with "Yes" and
> "No" with the initialize event. The only way I know how to do it is to use
> the following code for each ComboBox.
>
> With ComboBox#
> .AddItem "Yes"
> .AddItem "No"
> End With
>
> Is there simpler code to accomplish this?
>
> Thank you

 
Reply With Quote
 
r
Guest
Posts: n/a
 
      9th Jun 2009
assuming the names of the combo as:
ComboBox1, ComboBox2 ... ComboBox14

'in worksheet module
Sub Inizialize_Combo()
Dim oC As Object
For Each oC In ActiveSheet.OLEObjects
AddList oC
Next
End Sub

'in userform module
Private Sub UserForm_Initialize()
Dim oC As Object
For Each oC In Me.Controls
AddList oC
Next
End Sub

'in standard module
Sub AddList(cnt As Object)
Dim v
Dim RE As Object

Set RE = CreateObject("vbscript.regexp")
RE.ignorecase = True
RE.Pattern = "^combobox([1-9]|1[0-4])$"
v = Array("Yes", "No")
If RE.test(cnt.Name) Then
If TypeName(cnt) = "ComboBox" Then
cnt.List = v
ElseIf TypeName(cnt) = "OLEObject" Then
cnt.Object.List = v
End If
End If
End Sub

regards
r

Il mio ultimo lavoro ...
http://excelvba.altervista.org/blog/...ternative.html


"Johnny" wrote:

> I have 14 Combo Boxes on my Excel form that I want to populate with "Yes" and
> "No" with the initialize event. The only way I know how to do it is to use
> the following code for each ComboBox.
>
> With ComboBox#
> .AddItem "Yes"
> .AddItem "No"
> End With
>
> Is there simpler code to accomplish this?
>
> Thank you

 
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
Re: Listbox or Combobox? Need a drop down list but... Michael Bauer [MVP - Outlook] Microsoft Outlook VBA Programming 0 17th Jan 2009 06:06 PM
ComboBox drop list when clicked on??????????? Tdp Microsoft Excel Misc 6 13th Nov 2008 05:55 PM
F4 will not drop down list for combobox in datasheet view FlyingBuckner Microsoft Access 3 25th Mar 2008 12:01 PM
Combobox List Drop on Entry Daniel A. Microsoft Excel Programming 3 19th Dec 2007 01:35 PM
Combobox(Drop down list) flat style in ASP.NET =?Utf-8?B?a3Jpc2g=?= Microsoft Dot NET 1 5th Feb 2005 08:16 AM


Features
 

Advertising
 

Newsgroups
 


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