Urgent help needed - Change in userform...

B

barkiny

i have a userform. In that userform there are some optionbuttons an
checkboxes. And i have a textbox in which i see the results accordin
to above mentioned buttons.

I want this textbox updated whenever there is a change in optionbutton
or checkboxes (I dont want to press the OK button to make th
calculations)

For example if there is such a sub like "Private Su
Userform1_Change()" , i can use it. But i could not find such a sub

Please help
Thanks in advanc
 
H

Harald Staff

The boxes have Change and Clik events, call your code from those.

HTH. Best wishes Harald
 
B

barkiny

thanks for your reply

But i have too many checkboxes and optionbuttons.I dont want to writ
for each of them like

Private Sub CheckBox1_Change()
Private Sub CheckBox2_Change()
Private Sub CheckBox3_Change()
 
R

raypayette

I'm afraid you will have to have code for each control. Programming is
very precise.
If you think that is a lot of code, you should see programs in other
languages!
 
B

barkiny

thanks for your reply herald
As i said there are too many boxes, I dont want to call one by on
 
P

Peter T

Hi barkiny,

Search this ng for "WithEvents Checkbox", here's one pasted below -

" start copied post
Insert a class module named "Class1" and two command buttons near the bottom
of a userform.

'' start code in Class1
Public WithEvents cbx As MSForms.CheckBox

Private Sub cbx_Change()
MsgBox cbx.Tag & " : " & cbx.Value, , cbx.Name
'do stuff
End Sub

'' end code in Class1

''Start code in Userform1
Dim colClsChBoxes As New Collection

Private Sub CommandButton1_Click()
Dim cls As Class1
Dim cb As Control
For i = 1 To 3
Set cb = Me.Controls.Add("Forms.CheckBox.1", "My Check Box " & i, True)
With cb
..Left = 10
..Top = (i - 1) * 30 + 5
..Width = 90
..Height = 30
..Caption = .Name
End With
Set cls = New Class1
Set cls.cbx = cb


colClsChBoxes.Add cls
cb.Tag = colClsChBoxes.Count
Next


End Sub


Private Sub CommandButton2_Click()
Dim cnt As Long
Dim arr() As Boolean
cnt = colClsChBoxes.Count
If cnt Then
ReDim arr(1 To cnt)
For i = 1 To cnt
arr(i) = colClsChBoxes(i).cbx.Value
MsgBox arr(i), , colClsChBoxes(i).cbx.Name
Next
Else
MsgBox "No checkboxes in collection"
End If
End Sub


'' end code in Userform1

Class's for existing checkboxes could have course been instanciated and
added to the collection in the Intitialize event.

If you want to refer to the collection of class's (and hence checkboxes)
elsewhere in your project, declare colClsChBoxes as Public in a normal
module.
" end copied post

Now add a wide textbox named TexBox1, comment the msgbox line and replace
with

Dim s As String
s = cbx.Name & " Tag: " & cbx.Tag & " checked: " & cbx.Value
cbx.Parent.TextBox1.Text = s

Regards,
Peter T
 
H

Harald Staff

It's not a question of what you want, it's a question of what needs to be
done to make it work. If you don't want to do it, hire someone to do it for
you, you're probably sufficiently exhausted after typing these replies, even
though you saved a little energy skipping the Shift key here and there.

Best wishes Harald
 
P

Peter T

you're [OP] probably sufficiently exhausted after typing these replies,
even
though you saved a little energy skipping the Shift key here and there.

The OP did say this was urgent, so he may not have had time to use the Shift
key !

Regards,
Peter T

Harald Staff said:
It's not a question of what you want, it's a question of what needs to be
done to make it work. If you don't want to do it, hire someone to do it for
you, you're probably sufficiently exhausted after typing these replies, even
though you saved a little energy skipping the Shift key here and there.

Best wishes Harald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top