Using Exit Event to Format a Text Box

  • 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
    1. Private Sub tb_fixedfee_Exit()
    2. tb_fixedfee.Value = Format(tb_fixedfee.Value, "0.00%")
    3. End Sub
  • Re: Using Exit Event to Format a Text Box


    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.

  • Re: Using Exit Event to Format a Text Box


    Hello nrowader,


    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.


    [COLOR="navy"]How to use code tags[/COLOR]


    [noparse]

    Code
    1. [/noparse]
    2. [COLOR="navy"]your code goes between these tags[/COLOR]
    3. [noparse]

    [/noparse]
    ----------------------------------------------------------


    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.


    Code
    1. Private Sub TextBox1_LostFocus()
    2. With Me.TextBox1
    3. .Value = Format(.Value, "0.00%")
    4. End With
    5. End Sub


    If using a textbox control in the user form:


    Code
    1. Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    2. With Me.TextBox1
    3. .Value = Format(.Value, "0.00%")
    4. End With
    5. End Sub
  • Re: Using Exit Event to Format a Text Box