View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tim Williams Tim Williams is offline
external usenet poster
 
Posts: 1,588
Default passing ComboBox to subroutine

From within a form you should just be able to do:

selectID Me.ComboBoxName

That doesn't work for you ?

Tim


"Anand" wrote in message
...
On May 7, 5:16 am, Incidental wrote:
Hi Anand

I agree with Tim it would be good to see your code so far as it is a
lot easier to figure out how to help when you can see what you are
trying to do. However it sounds to me like you are trying to fill all
the comboboxes on your form one after an other??? The code below is
one way of doing just that, to test the code set up a userform with 4
comboboxes and a button leaving all the names as the default then
paste the code to the userform module. Then fill out some data in the
active sheet, column A will fill combobox1 - column B will fill
combobox2 etc.

I hope this gives you some idea of the method used.

Option Explicit
Dim Ctrl As Control
Dim i As Integer
Dim c As Integer

Private Sub CommandButton1_Click()

For i = 1 To 4 '4 is the number of comboboxes

Set Ctrl = UserForm1.Controls("ComboBox" & i)

For c = 1 To Cells(65536, i).End(xlUp).Row

Ctrl.AddItem (Cells(c, i))

Next c

Next i

Set Ctrl = Nothing

End Sub

I hope this helps

Steve


Hi,

Here is what I'm trying to do:

Public Sub selectID(cboBox) ' cboBox should be the combo box handle
Dim IDClm As Range
Dim LastRow As Long
Dim IDStr As String

Set IDClm = shtIDLists.Range(shtIDLists.Cells(4, 5),
shtIDLists.Cells(5000, 5))
LastRow = getLastUsedRow(IDClm) ' This is working subroutine
gives last used row
For lrow = 4 To LastRow
If Not IsEmpty(shtIDLists.Cells(lrow, 5)) Then
IDStr = shtIDLists.Cells(lrow, 5).Value
cboBox.AddItem IDStr
End If
Next lrow
End Sub

I want to call this subroutine from different forms:

Form1 - selectID(comboBox1);

Form2 - selectID(comboBox2);

How can I achieve this?

thanks,
Anand.