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
Related
I have two userforms. In userform1 the script that sets the value of "x" is:
Private Sub Combobox1_Change()
x = Combobox1.Value + 2
End Sub
I need userform2 to initialize with textbox1.value = x from userform1.
I don't even know where to start on this one. Any insight on how to do this?
EDIT: I'm trying the code below for userform2:
Private Sub UserForm_Initialize()
x = userform1.combobox1.Value
textbox1.Value = x
End Sub
The userform initializes fine, but the textbox1.Value working.
You can get the value from the combobox directly. It will have the values even if it is not visible any more.
UserForm2:
Private Sub UserForm_Initialize()
Me.TextBox1.Value = UserForm1.ComboBox1.Value + 2
End Sub
I also wrote to test it:
In a Modul:
Sub test()
UserForm1.Show 'I just had a fix value in the combobox for the test
UserForm2.Show
End Sub
I use an UserForm with 3 Text boxes.
TestBox1 for entering numeric values(LP).
TextBox2 for entring Discount in percentage and TextBox3 is for displaying the discounted value.
Now I want to get the Discount in percent if the user puts the discounted value in TextBox3.
l have tried Change event but both the Textbox values are changing.
Option Explicit
Dim Lp As Double
Dim Perc As Double
Private Sub Calculate()
Lp = CDbl(TextBox1.Value)
Perc = CDbl(TextBox2.Value)
TextBox3.Value = Lp - Lp * Perc / 100
End Sub
Private Sub TextBox2_Change()
TextBox2.Value = Lp - Lp * CDbl(TextBox1.Value) / 100
End Sub
Private Sub TextBox3_Change()
TextBox2.Value = (Lp - TextBox2.Value) * 100 / Lp
End Sub
Private Sub CommandButton1_Click()
Call Calculate
End Sub
Just use a procedure for calculation and run it in both change events of your textboxes.
Option Explicit
Private Sub TextBox1_Change()
CalculateResult
End Sub
Private Sub TextBox2_Change()
CalculateResult
End Sub
Private Sub CalculateResult()
If TextBox1.Value <> vbNullString And TextBox2.Value <> vbNullString Then
Textbox3.Value = TextBox1.Value * TextBox2.Value 'adjust your calculation
Else
Textbox3.Value = "fill box 1 and 2 first"
End If
End Sub
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
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.
I have a form with a number of text boxes for user input (this is in a User Form not on the spreadsheet). I have a few boxes that are related to currency and I need them to show the comma and decimal point as the user enters their criteria into the box. So far I have found a bunch of the same formulas online but when I input my number into the box it goes with 4.00 (if i hit 4 first) and all i can change after that is the second 0. Here is something similar I see online:
textbox1 = format(textbox1, "$#,##0.00")
Also seen some with cDbl
No matter what I try it won't let me enter anything more than the first number I enter. I need help. Thanks!
Formatting as the user types in data gets very tricky. May be better to format after the entry is complete.
Entry can also be validated and old value restored if entry deemed invalid
Dim TextBox1oldValue As String
Private Sub TextBox1_AfterUpdate()
If IsNumeric(TextBox1) Then
TextBox1 = Format(TextBox1, "$#,##0.00")
Else
TextBox1 = TextBox1oldValue
End If
End Sub
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(TextBox1) Then
TextBox1oldValue = Format(TextBox1, "$#,##0.00")
End If
End Sub
Private Sub UserForm_Initialize()
TextBox1oldValue = "$0.00"
TextBox1 = "$0.00"
End Sub
You need to use the TextBox Change event, like:
Private Sub TextBox1_Change()
If TextBox1 = vbNullString Then Exit Sub
If IsNumeric(TextBox1) Then CurrencyTransform(TextBox1)
End Sub
You then create the CurrencyTransform function to modify what it shows in the TextBox.
Try simply this...
Private sub textbox1_AfterUpdate()
textbox1 = format(textbox1, "$#,##0.00")
end sub
I wrote this inspired by chris' solution. It works while user is typing!
Private waiting As Boolean
Private Sub TextBox1_Change()
If waiting Then Exit Sub
waiting = True
TextBox1 = formatAsCurrency(TextBox1)
waiting = False
End Sub
Private Function formatAsCurrency(v As String)
If v = "" Then
formatAsCurrency = Format(0, "0.00")
Else
Dim vv As Variant
vv = Replace(v, ",", "")
vv = Replace(vv, ".", "")
formatAsCurrency = Format(vv / 100, "#,##0.00")
End If
End Function
Try this:
Private Sub TextBox1_Change()
TextBox1.Value = Format(TextBox1.Value, "$#,##0.00")
End Sub
This worked for me just fine, so it should help you as well.
If you want to do calculations that involve multiple text boxes, don't use .value after the name of the text box. Instead, use val( before the name of the text box while following it with an end parenthesis. I used .value and got weird results. Instead of, for example, $100 for TextBox1.Value + TextBox2.Value where TextBox1.Valueis equal to $25 and TextBox2.Value is equal to $75, I would get "$25$75".