+ Reply to Thread
Results 1 to 10 of 10

Using the Right function in VBA

  1. #1
    Registered User
    Join Date
    11-28-2016
    Location
    Seattle, WA
    MS-Off Ver
    Office 2003
    Posts
    4

    Using the Right function in VBA

    I have an Excel spreadsheet that contains, among other things a column that contains both names and Social Security numbers. I need to extract the Social Security numbers from each cell. My problem seems to be incorrect usage of the Right function.
    The code that I have written is:

    Dim SSN As String

    Range("A1").CurrentRegion.Columns(9).Select
    For Each cell In Selection
    SSN = RIGHT(RC[-6],9)

    MsgBox SSN
    Next cell

    End Sub

    The syntax for the Right function line is taken from a macro created while manually typing in the function to an Excel cell.

    This is just to test the concept. Ultimately, I need to put the Social Security number in each cell in a column distinct from the original column.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,028

    Re: Using the Right function in VBA

    For column A:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-28-2016
    Location
    Seattle, WA
    MS-Off Ver
    Office 2003
    Posts
    4

    Re: Using the Right function in VBA

    Thank you for the quick response. Unfortunately, I guess I was not clear about what I needed. The point of the code was to extract the information in Column 3 and put it into Column 9. Column 3 contains the name and Social Security number. Column 9 was to hold only the Social Security numbers. Thanks, in advance, for your reply.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,538

    Re: Using the Right function in VBA

    Can you give an example of what a cell in Column 3 looks like, or even better a sample workbook with desensitized data?
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,722

    Re: Using the Right function in VBA

    With a little arithmetic, this is the result
    Please Login or Register  to view this content.

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

    Re: Using the Right function in VBA

    No loop if you like
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Using the Right function in VBA

    No loop if you like
    Totally agree:

    Please Login or Register  to view this content.
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  8. #8
    Registered User
    Join Date
    11-28-2016
    Location
    Seattle, WA
    MS-Off Ver
    Office 2003
    Posts
    4

    Re: Using the Right function in VBA

    2468 Birnbaum, Roger 123456789

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Using the Right function in VBA

    As long as the data is in Col. C and you want the result in Col. I just change the 6 in my code to a 9.

  10. #10
    Registered User
    Join Date
    11-28-2016
    Location
    Seattle, WA
    MS-Off Ver
    Office 2003
    Posts
    4

    Re: Using the Right function in VBA

    Thank you. I have already counted the cells with data in the column, so I don't think that I need the first and second lines. Also, the header row was previously deleted so I changed "C2" to "C1". One of the issues that I had real trouble with was trying to essentially use a variable as the text string. None of the examples that I looked at online went beyond very simple use of the Right function. They all had a line in the code that set the text string to be equal to some variable and then used the variable in place of the text string.

+ 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. Calling function inside function. (aka nested function)
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2015, 05:58 AM
  2. Replies: 1
    Last Post: 06-10-2015, 12:56 PM
  3. Replies: 13
    Last Post: 04-08-2014, 05:46 AM
  4. index function error using match function to get data from a cell in an array
    By mabildgaard in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-17-2013, 02:21 AM
  5. [SOLVED] IF Function referencing IsNumber, Match, Left function on separate sheets
    By Touch9713 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-12-2013, 10:09 PM
  6. Replies: 1
    Last Post: 03-21-2012, 11:22 AM
  7. Replies: 2
    Last Post: 03-20-2009, 01:29 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