Results 1 to 4 of 4

Thread: [RESOLVED] Userform Textbox in percentage format

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    34

    Resolved [RESOLVED] Userform Textbox in percentage format

    I have a textbox in a userform so when the user keys in a number, it automatically converts it to a percentage. However, when the user keys in a 0, nothing populates in the textbox. See my code below and let me know what I need to do in order to allow the value 0 to be entered.

    Code:
    Private Sub txtratio_Change()
    txtratio.Value = Format(Val(txtratio.Value) / 100, "##%")
    txtratio.SelStart = Len(txtratio.Value) - 1
         
    End Sub

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Userform Textbox in percentage format

    Code:
    Private Sub txtratio_Change()
      txtratio.Value = Format(Val(txtratio.Value) / 100, "#0%")
      txtratio.SelStart = Len(txtratio.Value) - 1
    End Sub
    Use #0% (see above)

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    34

    Re: [RESOLVED] Userform Textbox in percentage format

    Thanks Enciv

  4. #4
    New Member
    Join Date
    Oct 2015
    Posts
    3

    Re: [RESOLVED] Userform Textbox in percentage format

    I know this is an old thread, but I found it while trying to debug a different root cause for the same symptom. So I'm posting my version of the problem and its solution for others who may wander in here with similar versions of this issue:

    If the Textbox control has its LinkedCell (or ControlSource) property set to refer to a worksheet cell, or if VBA code explicitly copies data to and from a worksheet cell, then the problem may be that Excel’s cell formatting system is overriding the Textbox’s value format. The fix for this problem is to ensure that the targeted LinkedCell (or ControlSource) cell, or the explicitly written/read cell, is formatted to Text format (“@”) or Percentage format (“0.00%”, “#%”, etc.).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width