PDA

View Full Version : Addin for work



Djblois
01-03-2007, 09:57 PM
I am taking XLGibbs advice and putting all my questions into this one post. Not all at once though. First, I want to start with 2 pretty simple things I am racking my brain with.

1) How do I get a textbox in a userform to be selected when the form is initialized, so you can just start typing?

2) How do I get it so when you are done typing in that same textbox, you just hit enter and the rest of my code will run?

Brandtrock
01-03-2007, 11:57 PM
Attached is a small example doing both of the things you asked.

Didn't know how you wanted the userform to activate. Just run it from Tools>Macro>Macros and select the RunUserform macro.

HTH,

Bob Phillips
01-04-2007, 03:57 AM
1) How do I get a textbox in a userform to be selected when the form is initialized, so you can just start typing?

Just set the control's SetFocus property



Me.TextBox1.SetFocus


If you want the text all pre-selected, you can also do that




With Me.TextBox1
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With



2) How do I get it so when you are done typing in that same textbox, you just hit enter and the rest of my code will run?

Use the Exit event of the textbox.



Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox "Exiting"
End Sub


This will also trigger on any exit action, tab or selecting another control, but it does give you a way to execute some code when exiting, and cancel that exit if necessary.

Djblois
01-04-2007, 05:28 AM
Thank you both for you help. XLD, the set focus works perfectly but the second part won't work for me because the textbox starts with the focus. If someone wants to change the settings now they would not be able to.

Thank you,
Daniel

Djblois
01-04-2007, 09:31 AM
Is there another way to get the enter key to work???

Zack Barresse
01-04-2007, 09:35 AM
Is there another way to get the enter key to work???
What do you mean?

TrippyTom
01-04-2007, 09:45 AM
I think what he's looking for is when you press Enter, it will effectively "click" the OK button to finalize the choices.

To do that, change the DEFAULT property on your OK button to TRUE.

lucas
01-04-2007, 09:47 AM
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
If KeyCode = 13 Then
KeyCode = 0
Sheet1.Range("A65536").End(xlUp).Offset(1, 0) = Me.TextBox1.Value
Me.TextBox1 = vbNullString
Me.TextBox1.SetFocus
End If

End Sub

Djblois
01-04-2007, 11:07 AM
Trippy Tom,

Thank you

Djblois
01-04-2007, 11:54 AM
Next,

I get this error sometimes, "Runtime error '1004': Unable to get the LabelRange Property of the Pivotfield class" Does anybody know what this means so I can debug it?