+ Reply to Thread
Results 1 to 8 of 8

Insert space if Textbox.value is certain length, remove space and last char with Backspace

  1. #1
    Registered User
    Join Date
    11-20-2020
    Location
    Québec, Canada
    MS-Off Ver
    MS Office 2013
    Posts
    19

    Insert space if Textbox.value is certain length, remove space and last char with Backspace

    Hi everyone,

    I'm making a payroll management system, complete with payslips, employee files, tax forms, punch system for employees, etc.

    To add a new employee to the system, I have a Userform full of text boxes, option buttons, list boxes... I have a textbox for Social Security Number. For the sake of readability, I want a space added after the first 3 digits then another after the next 3 digits, giving me something like ### ### ###. I managed to add the spaces with If statements in the textbox_Change sub but if I hit backspace, I can't get passed the spaces.

    Here's my code:
    Please Login or Register  to view this content.
    This causes the problem that when you encounter one of these spaces you get back to the length that requires a space, puts it back and you can't go back further. I tried using Textbox_KeyPress and Textbox_KeyDown to keep only the left 2 or 6 characters and end up with only the left 1 or 5 characters.

    Anyone has any idea how I could achieve this?
    Last edited by nightseeker; 02-28-2022 at 05:21 AM.

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

    Re: Insert space if Textbox.value is certain length, remove space and last char with Backs

    Just now I've tried with something like this :
    Please Login or Register  to view this content.
    2022-02-27_17-20-45.gif

    But I think it's not a good solution, because the code is not short.
    Last edited by karmapala; 02-27-2022 at 05:24 AM.

  3. #3
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Insert space if Textbox.value is certain length, remove space and last char with Backs

    Slightly different

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    11-20-2020
    Location
    Québec, Canada
    MS-Off Ver
    MS Office 2013
    Posts
    19

    Re: Insert space if Textbox.value is certain length, remove space and last char with Backs

    I think it's not a good solution, because the code is not short.
    @karmapala
    Wow, actually the length of the code isn't really a problem to me, as it happens "in real time" so I think you can barely see a delay. I like your solution and I can see that it's working.

    @JEC.
    This seems to work fine too. The way I understand it, it's pretty much karmapala's solution in one line. Am I right?

    Thank you for the replies. I will try them later today as I've been on the computer working on other parts of my project for HOURS, so my brain is fried right now.

    Now, say I would like to recreate it for another format (Postal Code here are written as A1A 1A1) how would I do that?
    Last edited by nightseeker; 02-27-2022 at 08:12 AM.

  5. #5
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Insert space if Textbox.value is certain length, remove space and last char with Backs

    Yes sort of, but more efficient

  6. #6
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,316

    Re: Insert space if Textbox.value is certain length, remove space and last char with Backs

    Possibly...
    code didn't test well.
    Last edited by dangelor; 02-27-2022 at 07:59 PM. Reason: Code failed

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

    Re: Insert space if Textbox.value is certain length, remove space and last char with Backs

    This only accepts numbers.
    Please Login or Register  to view this content.
    Quote Originally Posted by nightseeker‎
    Now, say I would like to recreate it for another format (Postal Code here are written as A1A 1A1) how would I do that?
    If you mean any 3 characters then
    Please Login or Register  to view this content.
    Last edited by jindon; 02-27-2022 at 08:17 PM.

  8. #8
    Registered User
    Join Date
    11-20-2020
    Location
    Québec, Canada
    MS-Off Ver
    MS Office 2013
    Posts
    19

    Re: Insert space if Textbox.value is certain length, remove space and last char with Backs

    Quote Originally Posted by JEC. View Post
    Slightly different

    Please Login or Register  to view this content.
    Ok so I tried this and had to tweak it a little because the first space did not disappear with the first digit of the second block. I changed it to this:
    Please Login or Register  to view this content.
    And it worked for the SSN.

    Attachment 770342

    But I think I will use @jindon solution instead as it also works for my Postal Code textbox. I post my part of code here because it could help someone eventually with their own issue.

    Thank you again for the replies. All the help is very much appreciated as always

+ 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] Remove space hyphen space and move data to right to new col
    By rz6657 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-20-2020, 02:02 PM
  2. Removing line space in Concatenate and Char(10) function
    By treesieg in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-18-2019, 11:43 AM
  3. [SOLVED] insert space in front of any question, if a space is not present
    By OhioRuss in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-19-2018, 06:45 PM
  4. Replies: 7
    Last Post: 03-09-2018, 11:02 AM
  5. Remove from over 2 blank space to 1 space.
    By calvinle in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-12-2018, 08:27 PM
  6. [SOLVED] How to remove empty space, unable to make pvot, because pvot calculate empty space
    By vengatvj in forum Excel Charting & Pivots
    Replies: 20
    Last Post: 10-29-2013, 12:43 PM
  7. [SOLVED] If Cell Starts with a Space, remove that space
    By Ocean Zhang in forum Excel General
    Replies: 2
    Last Post: 09-29-2012, 01:52 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