+ Reply to Thread
Results 1 to 15 of 15

Need assistance to format a userform textbox

  1. #1
    Registered User
    Join Date
    08-29-2018
    Location
    China
    MS-Off Ver
    Office 365
    Posts
    18

    Need assistance to format a userform textbox

    Hi VBA master,

    I need your help to format a textbox, where I would like the textbox to follow the structure of "##.###.###-###.###"

    I have two ideas, either when user input the number, it will follow the structure simultaneously or when the user click the textbox (keypressdown event), the format will show up like this __.___.___-___.___ and user just need to fill the blank.

    Hope you understand and can help me with it.

    Below is my current code that doesn't work....

    Thanks in advance


    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    05-14-2020
    Location
    Mauritius
    MS-Off Ver
    Office 365
    Posts
    97

    Re: Need assistance to format a userform textbox

    Hello,

    the way you think it is not possible. Not that easy at least. But here a possible way.
    I guess the string should contain 14 digits in the sequence e.g. 12.345.678-123.456
    Or also letters or a mix is possible because it will be always a string. I force the user to input min AND max 14 characters.
    In the After_Update event of the textbox I renew the string with the required format.
    See code:

    Please Login or Register  to view this content.
    Greetings

    Tor


  3. #3
    Registered User
    Join Date
    08-29-2018
    Location
    China
    MS-Off Ver
    Office 365
    Posts
    18

    Re: Need assistance to format a userform textbox

    Hi, so sad it cant live up to my expectation. But really appreciate for the alternative suggestion!!

  4. #4
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Need assistance to format a userform textbox

    Maybe try something like this ?
    Please Login or Register  to view this content.
    Last edited by karmapala; 06-11-2020 at 04:20 PM.

  5. #5
    Registered User
    Join Date
    08-29-2018
    Location
    China
    MS-Off Ver
    Office 365
    Posts
    18

    Re: Need assistance to format a userform textbox

    Hi!

    What a crack! Hahaha, thanks. However, I discovered one shortcoming by using that code.It doesn't allow us to erase the text in that textbox using backspace, because the . and - will still be there. Thus, create "Clear" function could be a solution somehow.

    Hahaha,

    Cheers and stay safe!

  6. #6
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Need assistance to format a userform textbox

    Quote Originally Posted by mbrillian View Post
    It doesn't allow us to erase the text in that textbox using backspace
    Maybe try like this to let the user use the backspace:
    Please Login or Register  to view this content.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Need assistance to format a userform textbox

    Place the code on top of your userform code module
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-29-2018
    Location
    China
    MS-Off Ver
    Office 365
    Posts
    18

    Re: Need assistance to format a userform textbox

    Hi Mr. Karmapala,

    Thanks in advance for your concern, but the code is not working, hehehe. Could it be because I modify a little of your previouse code? Here is the modified code to show "." and "-" :

    Private Sub Text_box_Change()
    If Len(Text_box.Value) = 2 Then Text_box.Value = Text_box.Value & "."
    If Len(Text_box.Value) = 6 Then Text_box.Value = Text_box.Value & "."
    If Len(Text_box.Value) = 10 Then Text_box.Value = Text_box.Value & "."
    If Len(Text_box.Value) = 12 Then Text_box.Value = Text_box.Value & "-"
    If Len(Text_box.Value) = 16 Then Text_box.Value = Text_box.Value & "."

  9. #9
    Registered User
    Join Date
    08-29-2018
    Location
    China
    MS-Off Ver
    Office 365
    Posts
    18

    Re: Need assistance to format a userform textbox

    Hi Mr. Jindom,

    Thanks for your response. I've tried your code but the shortcoming of cannot erase the value using a backspace still becomes an issue here. In fact, the code will not give me smooth input experience, because when user start type some characters, all the "." and "-" will show up

    But still appreciate your effort. Cheers

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Need assistance to format a userform textbox

    when user start type some characters, all the "." and "-" will show up
    Then simply...
    Please Login or Register  to view this content.
    Edit: Fixed a bug.
    Last edited by jindon; 06-16-2020 at 04:55 AM.

  11. #11
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Need assistance to format a userform textbox

    Hi mbrillian,

    Quote Originally Posted by mbrillian View Post
    Hi Mr. Karmapala,
    the code is not working, hehehe.
    The code in my post #4 is different than the code in my post #6.
    code post #4 put the "." or "-" before the next number
    ---> example : user type 1 then 2 then the "." show before the user type the third number --> 12.

    code post #6. put the "." or "-" after the next number
    ---> example : user type 1 then 2 then 3 then the "." show after the user type the third number --> 12.3

    Then the Sub TextBox1_KeyDown, is for the backspace.
    when the user hit the backspace once to erase the "." or the "-",
    the code double erase the character as if the user hit the backspace twice.

    Example :
    the display in the text box is : 12.3
    The user want to correct number 2,
    in a "real world" the user need to hit backspace 3 times so the number left is 1
    within the code, the user just need two times hit the backspace, the number left is 1.

    Could it be because I modify a little of your previouse code?
    It's not because you modify code post #4, but because the code is different than code post #6 .

  12. #12
    Registered User
    Join Date
    08-29-2018
    Location
    China
    MS-Off Ver
    Office 365
    Posts
    18

    Re: Need assistance to format a userform textbox

    Quote Originally Posted by karmapala View Post

    It's not because you modify code post #4, but because the code is different than code post #6 .
    Hi Sir,

    My bad, I thought its the extention code of previous code instead of new one.

    Meanwhile, I wanna confirm that the code works perfectly and has answered this post issue. Thanks

  13. #13
    Registered User
    Join Date
    08-29-2018
    Location
    China
    MS-Off Ver
    Office 365
    Posts
    18

    Re: Need assistance to format a userform textbox

    Hi Mr. Jindon,

    I've your code, and the backspace works, But this time the "." and "-" show up after we input 14 characters. Hehe. But worry not, that's other alternative that can be use. Thanks for your contribution btw.

  14. #14
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Need assistance to format a userform textbox

    Quote Originally Posted by mbrillian
    However if using my previous code of #,### and input "2,000"
    mbrillian, I don't exactly get what you mean on "2,000".
    Did you mean when the user input the number, he also hit the comma key on the computer keyboard ?
    A. So, he hit 2, hit comma, hit zero, hit zero, hit zero ....

    Or he input without hitting the comma key ?
    B. So, he hit 2, hit zero, hit zero, hit zero

    it will becomes 2 when I place the value of the textbox into a cell.
    If you want to put the value in the text box to a cell,
    assuming that the user input is in model B, then I think maybe use the code like this :

    Please Login or Register  to view this content.
    The CommandButton1_Click Sub is at the time to input what's in the text-box into a cell.
    Change the "Range("A1").Value" according to your situation.

    Is it possible to use "." in the formatting instead of ","
    Within the code, I think the formatting still need to use ",".
    But the display result show "." . Please have a look to the image below :

    2020-06-21_14-57-18.gif

    Please don't forget that your system for digit grouping is "." and for decimal point is ",".
    Also in Excel Option Advanced Editing Options, tick the Use System Separator.

  15. #15
    Registered User
    Join Date
    08-29-2018
    Location
    China
    MS-Off Ver
    Office 365
    Posts
    18

    Re: Need assistance to format a userform textbox

    Hi Mr. Karmapala!

    Thanks for the answer, the cdbl code is what I need.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Vba assistance with UserForm Textbox search code
    By sintek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-05-2016, 09:02 AM
  2. [SOLVED] Excel 2010 - Userform - display date from textbox in a label or textbox in 'ddd' format
    By theshybutterfly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-29-2015, 10:54 PM
  3. [SOLVED] Excel 2007, Userform Textbox Date Format and Calendar Control Userform
    By riffology in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-03-2014, 06:18 PM
  4. Userform.Textbox.Format
    By vin1 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-04-2011, 05:09 AM
  5. format a TextBox on a userform
    By randyvann in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-20-2008, 05:14 PM
  6. [SOLVED] format textbox in userform
    By jeffP in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2005, 10:07 PM
  7. [SOLVED] Format of a TextBox in a userform
    By MD in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-13-2005, 02:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1