View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Ed Sowell Ed Sowell is offline
external usenet poster
 
Posts: 17
Default How to Pass a Form to a Function - 2007

Aaron,
Since it's a Public function it can be called from the code for any form.

Also, you can refer to the controls of any form anywhere by using the form
name. E.g.,

frmMyForm.cboList

Ed

"Aaron" wrote in message
...
Hi,
Is there a way to pass a form to a function in VBA. For example, I have
created several userforms all of which have a combobox that I want filled
using the same set of code. Instead of including this code in each of the
forms code, I want to make this a function in a module and have the form
just
call the function to fill the combobox. The function would accept the form
name or whatever and use it to fill the combobox.
I am new to VBA and have tried using
Public Function FillSize(Obj As MSForms.UserForm)
For i = 1 To Obj.cmdSize.ListCount
'Removes an item from the Combo Box
Obj.cmdSize.RemoveItem 0
Next i

With Obj.cmdSize
.AddItem "AddStuff" 'Add to combo box by reading excel sheet

End With
End Function

And I would call the function using
FillSize(Me)
OR
FillSize(UserForm_Name)

Unfortunately I am not the most experienced of programmers and I am unsure
how to accomplish this, if my code is even a step in the right direction,
or
even if it possible to accomplish what Im asking.

Thanks for any help in advance,
--
Aaron