+ Reply to Thread
Results 1 to 12 of 12

VBA to replace 9 digit SSN with 4 digit - relative references

  1. #1
    Registered User
    Join Date
    02-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    2

    Question VBA to replace 9 digit SSN with 4 digit - relative references

    Hi, I've looked at previous threads about changing Social Security Numbers but haven't seen what I'm looking for. I need VBA code that I can distribute to multiple people who aren't great with excel in order to clean up 9 digit social security numbers and replace them with just the last 4 digits.

    I need the code to work across a variety of files, and to replace the original value, not just add a new column with the RIGHT(4). I also need it to work based on just the selected active column, rather than a predefined column reference. I thought I could do it with text to columns, but it appears to break down with values that don't include leading 0's.

    I've looked at the stuff in this thread excelforum.com/excel-programming-vba-macros/776671-shortening-a-social-security-number-to-last-4-digits.html but those seem to add a new column rather than replace the original.

    Any help would be tremendously appreciated!

    Thanks

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    So select the column:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    02-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    thank you!

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    You're welcome!

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  5. #5
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    Here is another macro (no looping) that you can consider...
    Please Login or Register  to view this content.
    Note the line of code I highlighted in red above. That line of code formats the cells as Text in order to preserve any leading zeros. If you want the values as real numbers that you custom format with 0000 as the Type pattern, then remove that line of code.

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    @ Rick - I noticed that we each named our routine the same - was that a Newton/Leibniz ?

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    No, I simply copied your posted code and then removed everything except for the first and last line (less typing for me and it saves me the trouble of having to figure out a name for the macro).

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    Awww -Shucks! I thought I was becoming important there for a brief instant.

  9. #9
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    Actually, I just had an idea for an even more compact, loopless macro (it's a one-liner), but it does not preserve leading zeros so you will have to custom format the cell to provide them...
    Please Login or Register  to view this content.

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    @ carnitastaco

    Here's Rick's code modified:

    Please Login or Register  to view this content.
    *The offset is for an imagined header in the first row

  11. #11
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    Actually, I would modify my Evaluate statement to recognize text as opposed to SSN's and ignore the text while processing the SSN's so that it automatically excludes headers (or any text values for that matter) if they should happen to be in the selection's column...
    Please Login or Register  to view this content.
    With that said, simply adding the EntireColumn reference to the Selection in the code I posted in Message #9 so that is automatically works on the entire column will automatically exclude headers (or any Text cells for that matter) provided the text in the headers do not have any dashes in them...
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 07-02-2020 at 02:52 PM.

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to replace 9 digit SSN with 4 digit - relative references

    Looks like the last one is a real winner as long as the numbers are entered with the dashes as they should be

+ 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. Replies: 6
    Last Post: 02-03-2019, 06:05 AM
  2. Bank Recon - How to convert 8 digit formatted check numbers to 6 digit
    By Quisp in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-21-2017, 09:55 PM
  3. [SOLVED] Formula to take off the last digit and replace it with a digit in another cell
    By Losguapos1 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-13-2017, 01:34 PM
  4. [SOLVED] LOTTERY FILTER#4, Find if 1 Digit Sum of 2 Digit or 3 Digit, Single Cell w/ dash
    By david gonzalez in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-08-2014, 12:57 AM
  5. Replace all one-digit number with two-digit numbers
    By sandykunaish in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-20-2013, 09:56 AM
  6. Replace first digit with a different digit??
    By taimysho0 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-01-2012, 05:46 PM
  7. Replies: 2
    Last Post: 06-17-2010, 08:36 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