Is there an in-built numeric updown control in vba or do we need to create a control like that?
If there is such a control then what are the events that we may use.
Pls suggest.
You can use the SpinButton1 control for that
SNAPSHOT
CODE
You can either set the min and max of the SpinButton1 in design time or at runtime as shown below.
Private Sub UserForm_Initialize()
SpinButton1.Min = 0
SpinButton1.Max = 100
End Sub
Private Sub SpinButton1_Change()
TextBox1.Text = SpinButton1.Value
End Sub
FOLLOWUP
If you want to increase or decrease the value of the textbox based on what user has input in the textbox then use this. This also makes the textbox a "Number Only" textbox which just fulfills your other request ;)
Private Sub SpinButton1_SpinDown()
TextBox1.Text = Val(TextBox1.Text) - 1
End Sub
Private Sub SpinButton1_SpinUp()
TextBox1.Text = Val(TextBox1.Text) + 1
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case vbKey0 To vbKey9, 8
Case Else
KeyAscii = 0
Beep
End Select
End Sub
Related
So currently, I have the following in a Userbox in VBA; a checkbox and a close window button.
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then vb1 = -1 Else vb1 = 1
End Sub
Private Sub CommandButton1_Click()
UserForm1.Hide
End Sub
And I have this in my Module.
Option Explicit
Sub GetRangeData_inputbox()
Dim vb1, vb2, vb3, vb4, vb5, vb6, vb7 As Integer
Range("c3") = vb1 * 1000
The primary purpose of the userbox is to check a box. I opted for a userbox since there are many of these boxes to be checked, but I only put one in for simplistic purposes.
The Goal of this is for a box to be checked, change vb1 to -1, or vb1 to 1 if the box is checked or unchecked, then, in the module, flow into a formula that does 1,000 (for example)*VB1.
Let me know,
Thanks!
To use a variable in a sub you must declare it like this Sub GetRangeData_inputbox(vb1 As Integer) and then you must call the sub in the CheckBox1_Click() event.
Here is a proof of concept:
Private Sub CheckBox1_Click()
Dim vb1 As Integer
If CheckBox1.Value = True Then vb1 = -1 Else vb1 = 1
Call GetRangeData_inputbox(vb1)
End Sub
And in the module:
Sub GetRangeData_inputbox(vb1 As Integer)
Range("c3") = vb1 * 1000
End Sub
Also you can replace UserForm1.Hide with UserForm1.Unload, unless you have a reason for hiding it.
I'm trying to validate textboxes in a UserForm. I'm using this solution on my worksheet.
How can I add validation to my textboxes to only accept numbers between 60 to 100 using the given solution? Should I add something after the If KeyAscii >= 48 And KeyAscii <= 57 Then or it should be elsewhere?
UserForm:
Class code:
Private WithEvents tb As MSForms.TextBox 'note the "WithEvents"
Sub Init(tbox As Object)
Set tb = tbox 'assigns the textbox to the "tb" global
End Sub
'Event handler works as in a form (you should get choices for "tb" in the
' drop-downs at the top of the class module)
Private Sub tb_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii >= 48 And KeyAscii <= 57 Then
Debug.Print tb.Name, "number"
Else
MsgBox "The value should be in number only!", vbOKOnly + vbCritical, "Error"
Debug.Print tb.Name, "other"
KeyAscii = 0
End If
End Sub
Code on UserForm:
Private colTB As Collection 'holds your class instances
' and keeps them in scope
'This performs the setup
Private Sub UserForm_Activate()
Dim c As Object
Set colTB = New Collection
'loop all controls in the frame
For Each c In Me.Frame3.Controls
'look for text boxes
If TypeName(c) = "TextBox" Then
Debug.Print "setting up " & c.Name
colTB.Add TbHandler(c) ' create and store an instance of your class
End If
Next c
End Sub
' "factory" method
Private Function TbHandler(tb As Object) As clsTxt
Dim o As New clsTxt
o.Init tb
Set TbHandler = o
End Function
I don't think you can achieve that easily using a Class. The solution you are using will help you monitor each keystroke, but it looks as though you are wanting to validate input after the user has finished with the field and then you need to verify that what he has input is between 60 and 100. For that you would probably be looking at the AfterUpdate event but sadly that is not available within a class.
I think you will either need to create a stub for each textbox_AfterUpdate to do the validation.
Try this:
Private Sub tb_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsNumeric(tb.Value) Then
'MsgBox "only numbers between 60 and 100 are allowed"
tb.Value = ""
ElseIf tb.Value >= 60 And tb.Value <= 100 Then
'MsgBox "OK!"
Else
'MsgBox "only numbers between 60 and 100 are allowed"
tb.Value = ""
End If
End Sub
I created a spin button on a user form however the min and max i'd like to set it to will not work when I change the properties of the spin button to a 10 digit value. I also tried to manually change it in the code but it would not allow me to run the code with the 10 digit min and max. It kept putting # after the 10 digit min and max then said error 424 when I tried to run it anyway. How can I change the properties of a spin button on a userform to 10-digit values?
Please try to explain the easiest way to do this as I'm not a pro coder or anything lol
Use this code in userform:
Private Sub TextBox1_AfterUpdate()
If TextBox1.Value > 9999999999# Then
TextBox1.Value = 9999999999#
ElseIf TextBox1.Value < 1000000000 Then
TextBox1.Value = 1000000000
End If
End Sub
Private Sub UserForm_Initialize()
TextBox1.Value = 1000000000
End Sub
Private Sub SpinButton1_SpinDown()
If TextBox1.Value <> 1000000000 Then
TextBox1.Value = TextBox1.Value - 1
End If
End Sub
Private Sub SpinButton1_SpinUp()
If TextBox1.Value <> 9999999999# Then
TextBox1.Value = TextBox1.Value + 1
End If
End Sub
I have one UserForm with 1 TextBox and 1 ComboBox.
I firstly write in the ComboBox (per exemple Sarah)
Private Sub ComboBox1_Change()
ThisWorkbook.Sheets("CalculSheet").Range("A2").Value = ComboBox1.Text
End Sub
Then it makes some calcul in A3 like (If D2=Sarah Then D3=1)
Private Sub UserForm_Active()
Application.ScreenUpdating = False
ThisWorkbook.Worksheets("CalculSheet").Activate
TextBox1 = Range("A3").Value
End Sub
And I want that the Result comes directly in my TextBox1. It means that I write Sarah in the ComboBox1 and directly comes 1 in the TextBox1.
Delete the Private Sub UserForm_Active() code. You don't need that. Replace ComboBox1_Change() with this.
Is this what you are trying? (Untested)
Private Sub ComboBox1_Change()
With ThisWorkbook.Sheets("CalculSheet")
.Range("A2").Value = ComboBox1.Text
DoEvents
TextBox1.Text = .Range("A3").Value
End With
End Sub
I have a textbox on a userform. It is the only textbox on the form. There are three labels and two buttons in addition to this textbox. Basically, I want the focus to remain on this textbox under all scenarios, other than the moment that one of the buttons would be clicked, but then I want the focus to come right back to the text box. Both buttons have "TakeFocusOnClick" and "TabStop" set to False. I was having problems with getting the focus set to the textbox, which is why I changed these two settings.
Once I changed these settings, the Enter key in the textbox stopped having any effect. I have events written for _AfterUpdate and _KeyPress for the textbox, but they don't fire. As you can see in the code, I have commented out the lines to set the focus to this textbox. Since it is now the only object that can take focus, these lines are not needed (theoretically). When I allowed the other objects to take focus, these lines weren't having any effect (focus was switching to the buttons despite these SetFocus lines).
Here is the code. It is very simple, except that the Enter key isn't triggering the event. Can anyone see why? Thanks.
Private Sub btnDone_Click()
Application.Calculation = xlCalculationAutomatic
formMath.Hide
'Clear statistics
Range("attempts").Value = 0
Range("correct").Value = 0
Sheet5.Range("A2:W500").ClearContents
End Sub
Private Sub btnSubmit_Click()
recordAnswer
'formMath.txtAnswer.SetFocus
End Sub
Private Sub txtAnswer_AfterUpdate()
recordAnswer
'formMath.txtAnswer.SetFocus
End Sub
Private Sub txtAnswer_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then
recordAnswer
End If
End Sub
Private Sub UserForm_Initialize()
'Initialize manual calculation
Application.Calculation = xlCalculationManual
Application.Calculate
'Initialize statistics
Range("attempts").Value = 0
Range("correct").Value = 0
Sheet5.Range("A2:W500").ClearContents
'Initialize first problem
newProblem
End Sub
Sub recordAnswer()
'Update statistics
Dim attempts, correct As Integer
attempts = Range("attempts").Value
correct = Range("correct").Value
Range("results").Offset(attempts, 0).Value = attempts + 1
Range("results").Offset(attempts, 1).Value = lblTopNum.Caption
Range("results").Offset(attempts, 2).Value = lblBotNum.Caption
Range("results").Offset(attempts, 3).Value = lblBop.Caption
Range("results").Offset(attempts, 4).Value = Range("Answer").Value
Range("results").Offset(attempts, 5).Value = txtAnswer.Text
If (Range("Answer").Value = txtAnswer.Text) Then
Range("results").Offset(attempts, 6).Value = 1
Else
Range("results").Offset(attempts, 6).Value = 0
End If
'Update attempts and success
Range("attempts").Value = attempts + 1
Range("correct").Value = correct + 1
newProblem
End Sub
Sub newProblem()
Application.Calculate
formMath.lblTopNum.Caption = Range("TopNum").Value
formMath.lblBotNum.Caption = Range("BotNum").Value
formMath.lblBop.Caption = Range("ProbType").Value
formMath.txtAnswer.Value = ""
'formMath.txtAnswer.SetFocus
End Sub
To start off
You can either in the design mode, set the TabIndex property of the Textbox to 0 or you can set the focus on the textbox in the UserForm_Initialize()
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
Similarly after any operation that you perform, simply call the TextBox1.SetFocus to revert to the textbox.
Option Explicit
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
Private Sub CommandButton1_Click()
MsgBox "Hello from Button 1"
TextBox1.SetFocus
End Sub
Private Sub CommandButton2_Click()
MsgBox "Hello from Button 2"
TextBox1.SetFocus
End Sub
Let me know if this is not what you want?
I found a way to accomplish this. In the code above I took out the _KeyPress and _AfterUpdate events and replaced them with:
Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 13: recordAnswer
End Select
End Sub
Not sure why the other methods didn't work, but this does.
Also not sure why simply setting the focus directly didn't work. I suspect that the focus was being set, but then something else was happening subsequently that was changing the focus off of the textbox. Just a guess.
Thanks for the help. I appreciate it.