Announcement

Collapse
No announcement yet.

Using Exit Event to Format a Text Box

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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:
    Private Sub tb_fixedfee_Exit()
    
    tb_fixedfee.Value = Format(tb_fixedfee.Value, "0.00%")
    
    End Sub
    Last edited by AAE; March 26th, 2011, 10:46. Reason: ADD CODE TAGS

  • #2
    Re: Using Exit Event to Format a Text Box

    How about:

    Code:
       tb_fixedfee = Format(Val(tb_fixedfee), "0.00%")

    Comment


    • #3
      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.

      Comment


      • #4
        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.

        How to use code tags

        [code]
        your code goes between these tags
        [/code]
        ----------------------------------------------------------

        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:
        Private Sub TextBox1_LostFocus()
            With Me.TextBox1
                .Value = Format(.Value, "0.00%")
            End With
        End Sub
        If using a textbox control in the user form:

        Code:
        Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        
            With Me.TextBox1
                .Value = Format(.Value, "0.00%")
            End With
        
        End Sub
        AAE
        ----------------------------------------------------

        Forum Rules | Message to Cross Posters | How to use Tags

        Comment


        • #5
          Re: Using Exit Event to Format a Text Box

          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
          Reafidy

          Forum Rules | Anatomy Of A Good Thread Title | What Have You Tried So Far? | Are You A Cross Poster?

          Comment


          • #6
            Re: Using Exit Event to Format a Text Box

            Thanks for both ideas, that did it.

            Comment

            Working...
            X