View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default initialize userform, using a sub

He is not passing them between the procedures, but red-defining new ones in
each procedure!

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Mike Fogleman" wrote in message
...
In order to pass variables between procedures in the same module, they

need
to be DIMensioned outside of the procedures, usually at the top of a

regular
code module:
Option Explicit
Dim WT as Range

Sub MySub ()
........
End Sub

To pass variables between modules, including UserForms, Dim them as above,
but as Public:
Option Explicit
Public WT as Range

Sub MySub ()
........
End Sub

That will pass variables back to the UserForm.
Mike F
"natanz" wrote in message
oups.com...
I have many userforms in the macro that i am writing. most of the
initialization is the same, populating a bunch of comboboxes from a
range.

currently i am doing the initialization in the code window of all the
userforms, but i thought it would be more efficient to call a procedure
in module1 to initialize the form. but this is not working.

here is the original code from the code window of the form:

Private Sub UserForm_Initialize()
Dim ctl As Control
Dim WT
Dim DT

WT = Sheets("sheet2").Range("W_T")
DT = Sheets("sheet2").Range("D_T")

Me.Caption = ActiveCell.Value
For Each ctl In Me.Controls
If TypeName(ctl) = "ComboBox" Then
ctl.List = WT
ctl.ListIndex = 0
End If
Next ctl

ComboBox12.List = DT
ComboBox12.ListIndex = 0
ComboBox23.List = DT
ComboBox23.ListIndex = 0

TextBox1.Value = 0
end sub

here is what i changed it to:
Private Sub UserForm_Initialize()
Call init_cboxes(pg_a2)
Dim WT
Dim DT

WT = Sheets("sheet2").Range("W_T")
DT = Sheets("sheet2").Range("D_T")

ComboBox7.List = DT
ComboBox7.ListIndex = 0

TextBox1.Value = 0
End Sub

where sub init_cboxes looks like this:

Public Sub init_cboxes(ByVal MyForm As UserForm)
Dim ctl As Control
Dim WT
Dim DT

WT = Sheets("sheet2").Range("W_T")
DT = Sheets("sheet2").Range("D_T")

MyForm.Caption = ActiveCell.Value
For Each ctl In MyForm.Controls
If TypeName(ctl) = "ComboBox" Then
ctl.List = WT
ctl.ListIndex = 0
End If
Next ctl
End Sub

the code runs, but it doesn't seem to pass the values back to the
userform. Is it possible to do this?