I am working on an Excel userform with a textbox for the user to enter a dollar amount that was either received or given. That number is put into a spreadsheet cell after the command button is selected. I have two radio buttons, one is for money given and the other one is for money received.
I would like the user to enter a positive amount in the box and when the given button is selected it turns into a negative. When the received button is selected it will leave the box as positive.
Normally I would multiply something by -1 to turn it negative. I tried userform.textbox.value = userform.textbox.value * -1
.
Private Sub InsTxtBox_AfterUpdate()
GameLogUF.InsTxtBox = Format(GameLogUF.InsTxtBox, "$#,###")
If GameLogUF.InsPlyrOptnBtn.Value = True Then
GameLogUF.InsTxtBox.Value = GameLogUF.InsTxtBox.Value * -1
End If
End Sub
First of all, check if you trigger the event. Put a breakpoint in this sub and try to change the value.
The AfterUpdate event is triggered when control or record is updated. Within a record, changed data in each control is updated when the control loses the focus or when the user presses Enter or Tab.
Just checked your code and it works if the event was triggered.
Also, I suggest to change it a bit.
Dim value As Double
Dim tryValue As String
'remove $ sign so String value could be converted to Number format
tryValue = Replace(GameLogUF.InsTxtBox.value, "$", "")
'if user input is not numeric after removing $ sign - input incorrect
If Not IsNumeric(tryValue) Then
'do sth else
Exit Sub
End If
value = tryValue
' "= True" part is not needed there is no need to compare boolean property '
If GameLogUF.InsPlyrOptnBtn.value Then
'if user input is negative your code will turn it back to positive, with Abs it always will be negative in this case'
value = Abs(value) * -1
End If
'do formatting in the end'
GameLogUF.InsTxtBox.value = Format(value, "$#,###")