VBA Text Box displaying Currency - excel

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".

Related

Userform - Textbox only accept number

I've been searching for a solution to change apperance of my textbox into red when the entered value is not a number.
Actually, I succeeded with a textbox only but I have 14 textboxes at total. I've tried with the codes below:
Private Sub Justi_num()
Dim i As Integer
For i = 2 To 14
If IsNumeric(Controls("TextBox" & i).Value) Then
Controls("TextBox" & i).BackColor = RGB(255, 255, 255) 'Blanc
Else 'Sinon
Controls("TextBox" & i).BackColor = RGB(247, 205, 201) 'Rouge clair
End If
Next i
End Sub
But it didnt work. Any help? Thanks in advance.
You first need to ensure that your code is called whenever the user enters something. This is done with Event-Routines. Controls (like buttons, checkboxes, textboxes and so on) have different events like change (something was changed), dblClick (there was a double click on the control) and so on.
If you want some code to be executed, you need to put code into that event routine. The name of the event routine is combined by the name of the control and the event name. If the name is not following that rule, the code is not called automatically.
In design mode, go to the form editor and double click onto one of your textboxes: The editor will open the text editor window and create a subroutine with the most common event of that control (for a textbox, that's the Change-event, for a button, it would be the Click-event):
Private Sub TextBox1_Change()
End Sub
You could call your routine Justi_num from this routine:
Private Sub TextBox1_Change()
Justi_num
End Sub
However, you will need to create such an event routine for every of your text boxes, there is no way around that (ok, yes, there is, but it is much too complicated for a beginner).
Private Sub TextBox2_Change()
Justi_num
End Sub
Private Sub TextBox3_Change()
Justi_num
End Sub
and so on...
One thing: Your routine Justi_num handles all textboxes, no matter which textbox was changed. You could change the code so that the event routine passes the information which control was changed and the color changing routine handles only that control:
Private Sub TextBox1_Change()
setTextBoxColor Me.TextBox1
End Sub
Private Sub TextBox2_Change()
setTextBoxColor Me.TextBox2
End Sub
(and so on...)
Private Sub setTextBoxColor(tbox As Control)
If IsNumeric(tbox.Value) Or tbox.Value = "" Then
tbox.BackColor = vbWhite
Else
tbox.BackColor = RGB(247, 205, 201)
End If
End Sub
I'll try to help:
One thing that may fail is that "IsNumeric" gives a boolean result. Try using If IsNumeric(Controls("TextBox" & i).Value) = True Then
It would be something like:
Private Sub Justi_num()
Dim i As Integer
Dim NA As Boolean
NA = False
For i = 2 To 14
NA = IsNumeric(Controls("TextBox" & i).Value)
If NA = True Then
Controls("TextBox" & i).BackColor = RGB(255, 255, 255) 'Blanc
Else 'Sinon
Controls("TextBox" & i).BackColor = RGB(247, 205, 201) 'Rouge clair
End If
Next i
End Sub
Hope it helps!

Data entry from Checkbox to Textbox

I am experimenting with the capabilities of a User Form to assist in Data entry. I would like to know if there is a specific code that can transfer the Value of a Checkbox to a Text box that is on the same User Form when the box is checked. Basically, instead of having to type all of the words out, it would be easier to simply check a series of boxes to create the sentence. I know how to input the Checkbox value into the Excel worksheet but I have yet to figure out how to have that same value entered into a Text Box that would provide a "Preview" of the sentence for editing purposes and then the data can be transferred to the Excel Worksheet once it has been approved in the User Form. In my attached example that I had created and I have a before and the desired after result of what I am looking to do.
Thank you
JLY Test form:
EDIT:
Though really, you'd be best off using a listbox, much easier and shorter code. Put your list of items in a worksheet and set that to a dynamic named range so you can edit it and the userform will pick it up on the fly. Make sure the listbox has a MultiSelect property set to fmMultiSelectExtended and the ListStyle property is set to fmListStyleOption. Then you can select multiple entries in the listbox by holding the Ctrl key.
In this example if have put it in Sheet1 (though can be any sheet, and the sheet can even be hidden), and then set a dynamic named range named listProperties to this formula: =Sheet1!$A$2:INDEX(Sheet1!$A:$A,MAX(2,ROWS(Sheet1!$A:$A)-COUNTBLANK(Sheet1!$A:$A)))
Then the userform has this code:
Private Sub listJewelryProperties_Change()
Dim sTemp As String
Dim i As Long
For i = 0 To Me.listJewelryProperties.ListCount - 1
If Me.listJewelryProperties.Selected(i) = True Then sTemp = sTemp & " " & Me.listJewelryProperties.List(i)
Next i
Me.txtPreview.Text = WorksheetFunction.Trim(sTemp)
End Sub
Private Sub UserForm_Initialize()
Me.listJewelryProperties.Clear
Me.listJewelryProperties.List = ActiveWorkbook.Sheets("Sheet1").Range("listProperties").Value
End Sub
And this is what the results look like:
Original Answer Here for Posterity:
Alternate solution, set all your checkboxes to call a function and pass an argument of their value and caption, then the function will update the textbox. The reason for using the .Tag property is to avoid removing too much due to duplicates in partial matches for the checkboxes (such as Ring and Ring Band where just plain Ring can be found within Ring Band, this way it will only remove the Ring entry, and not incorrectly remove both Ring entries)
Private Sub chk14ktWhiteGold_Click()
UpdatePreview Me.chk14ktWhiteGold.Value, Me.chk14ktWhiteGold.Caption
End Sub
Private Sub chkAntique_Click()
UpdatePreview Me.chkAntique.Value, Me.chkAntique.Caption
End Sub
Private Sub chkArtisinal_Click()
UpdatePreview Me.chkArtisinal.Value, Me.chkArtisinal.Caption
End Sub
Private Sub chkBand_Click()
UpdatePreview Me.chkBand.Value, Me.chkBand.Caption
End Sub
Private Sub chkHandCarved_Click()
UpdatePreview Me.chkHandCarved.Value, Me.chkHandCarved.Caption
End Sub
Private Sub chkHandEtched_Click()
UpdatePreview Me.chkHandEtched.Value, Me.chkHandEtched.Caption
End Sub
Private Sub chkHandmade_Click()
UpdatePreview Me.chkHandmade.Value, Me.chkHandmade.Caption
End Sub
Private Sub chkRing_Click()
UpdatePreview Me.chkRing.Value, Me.chkRing.Caption
End Sub
Private Sub chkRingBand_Click()
UpdatePreview Me.chkRingBand.Value, Me.chkRingBand.Caption
End Sub
Private Sub chkSterlingSilver_Click()
UpdatePreview Me.chkSterlingSilver.Value, Me.chkSterlingSilver.Caption
End Sub
Private Sub chkVintage_Click()
UpdatePreview Me.chkVintage.Value, Me.chkVintage.Caption
End Sub
Private Sub UpdatePreview(ByVal bChkState As Boolean, ByVal arg_sText As String)
If bChkState = True Then
Me.txtPreview.Text = WorksheetFunction.Trim(Me.txtPreview.Text & " " & arg_sText)
If Len(Me.txtPreview.Tag) = 0 Then
Me.txtPreview.Tag = arg_sText
Else
Me.txtPreview.Tag = Me.txtPreview.Tag & "|" & arg_sText
End If
Else
Me.txtPreview.Tag = Replace("|" & Me.txtPreview.Tag & "|", "|" & arg_sText & "|", "|")
Me.txtPreview.Text = WorksheetFunction.Trim(Replace(Me.txtPreview.Tag, "|", " "))
End If
End Sub
Something like below will do that for you, as I don't know the names of your UserForm or your Checkboxes, you will have to amend as required, also you will have to add the following code to each of the CheckBoxes Click Event:
Private Sub CheckBox1_Click()
UserForm1.TextBox1.Text = UserForm1.TextBox1.Text & " " & CheckBox1.Caption
End Sub
UPDATE:
To also remove when checkbox is unchecked, the following code will do that:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
UserForm1.TextBox1.Text = UserForm1.TextBox1.Text & " " & CheckBox1.Caption
Else
pos = InStr(UserForm1.TextBox1.Text, CheckBox1.Caption)
If pos > 0 Then UserForm1.TextBox1.Text = Replace(UserForm1.TextBox1.Text, " " & CheckBox1.Caption, "")
End If
End Sub

When I write in a TextBox with Calcul behind then comes directly the Result in another TextBox

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

OptionButton determines value then use in macro

A basic simple VBA question that I havent been able to work out even though there are numerous tutorials about it.
I want a Userform to pop-up, give you two options (OptionButton1 and OptionButton2). You choose one, click ok. And depending on what Option is chosen a certain value is used (in an email, for which I DID finish the macro). For simplifying purposes i now just want the variable 'contents' to be printed in cell A1.
I have these parts so far:
Sub MailOption()
UserForm1.Show
End Sub
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then Var1 = "Text example 1"
End Sub
Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then Var1 = "Text example 2"
End Sub
Private Sub SendEmail_Click()
Cells(1, 1).Value = Var1
End Sub
There are multiple problems: the variable is not shown in Cell A1 and when i press Send Email the Form is not closed. I am probably doing lots of stuff wrong but its the first time im using a userform. Ty very much
I would simply use this one without handling OptionButton_Click:
Private Sub SendEmail_Click()
Dim res As String
If OptionButton1.Value Then
res = "Text example 1"
ElseIf OptionButton2.Value Then
res = "Text example 2"
Else
res = "Nothing is selected"
End If
'write result in cell
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = res
'close form
Unload Me
End Sub

Excel Userform Textbox Behavior

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.

Resources