UserForm

G

Guest

I have I user form that is shown when the user presses a button on a
spreadsheet.
The code in the spreasheet is:

Public Sub startKnapp_Click()
userForm1.Show
End Sub

Then the userform is shown. I want to do certain things when the userform is
opened etc. count number of textboxes. How do you write code so that the
program does certain things directly when the userform is opened i.e. the
user shall not be forced to press a button or move the cursor over certain
areas etc.? Please help me if you know how! Thanks!
 
N

NickHK

The Userform has various events that you can respond to. I would
UserForm_Activate() and UserForm_Initialize() are the ones you are looking
for.

NickHK
 
G

Guest

Thank you. I am however not very accostomed to VB. How do you the
UserForm_Activate() and UserForm_Initialize() ? Do you write:

Private sub UserForm_Activate()
......count checkboxes
End sub

or how do you use it? Would be very much appreciated with help since I dont
really know my way around these blocks....

"NickHK" skrev:
 
N

NickHK

Nick,
Yes, you would need code like:
Private sub UserForm_Initialize()
'Code Here e.g.
TextBox1.SetFocus
End sub

The _Initialize event is fired when the Userform is first shown and the
_Activate event fires whenever the form receives focus.
Check out the Help for (in English) "Understanding Objects, Properties,
Methods, and Events"

NickHK
 
G

Guest

Thanks! I seem however to have two problems. I thought I knew how to count
the textboxes in a frame but I have a problem with my code:

numberOfTextBoxes = StartPriceFrame.TextBoxes.Count

It simple does not work. Do you perhaps know how to write the code for this?

The second problem is that I need to use the value for the number of
textboxes in another sub in the userform (e.g. there are five textboxes i.e.
the number 5). But the varibale numberOfTextBoxes dies after the initialize
sub. Can you send this variable along or should I declare it is a modular
variable? I would be very thankful if you could help me with this!

"NickHK" skrev:
 
N

NickHK

Nick,
Yes, declare it the general section so it live as long as the form is loaded
Dim numberOfTextBoxes As Long
Private Sub UserForm_Initialize()
Dim cntrl As Control
For Each cntrl In Me.Controls
If TypeName(cntrl) = "TextBox" And cntrl.Parent Is Frame1 Then
numberOfTextBoxes = numberOfTextBoxes + 1
End If
Next
End Sub

NickHK
 

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