Code:tb_fixedfee = Format(Val(tb_fixedfee), "0.00%")
I am trying automatically to format a number in a text box as a percentage. I am trying to use the exit event to trigger the format but it doesn't seem to do anything. Any ideas? Here is the code I wrote. The text box is called tb_fixedfee.
Code:Private Sub tb_fixedfee_Exit() tb_fixedfee.Value = Format(tb_fixedfee.Value, "0.00%") End Sub
Last edited by AAE; March 26th, 2011 at 11:46. Reason: ADD CODE TAGS
Thanks. Actually to be more specific I can't get the exit event to trigger so no matter what is after the event it doesn't trigger. I am thinking it is something in the properties for the text box, but I am really not sure.
Welcome to Ozgrid.
While we're glad to have you on-board, please take some time to read through the Forum Rules.
All VBA code posted in the forum must be wrapped in code tags which you omitted. I've added the tags for you this time only. Be sure to use them in future posts.
How to use code tags
your code goes between these tags
There is a slight difference in the text box properties depending on whether you are using the textbox embedded into the worksheet via the Control Box controls or using the control on a user form. There is not a an Exit_Event for textbox controls embedded into the worksheet sheet (at least not in my version of Excel/VBA). However, you could use the lostfocue event.
If using a textbox control in the user form:Code:Private Sub TextBox1_LostFocus() With Me.TextBox1 .Value = Format(.Value, "0.00%") End With End Sub
Code:Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) With Me.TextBox1 .Value = Format(.Value, "0.00%") End With End Sub
Code:Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean) With Me.TextBox1 If IsNumeric(.Text) Then .Value = Format(.Value, "0.00%") Else Cancel = True End If End With End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)