+ Reply to Thread
Results 1 to 8 of 8

Help to return only first 12 characters when a cell contains specific text

  1. #1
    Registered User
    Join Date
    09-05-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    23

    Help to return only first 12 characters when a cell contains specific text

    Hi Guys,
    I have a spreadsheet which has some unnecessary characters added onto the end of some records which I need to remove. I am essentially trying to ensure that if a Cell in column A begins with (or contains) "PR" that it only returns the first 12 characters. Other records in column A may remain longer than 12 characters if they don't begin with "PR".

    I have written the below code based off of a number of different google searches and some of my very limited knowledge of VBA and it is successfully returning only 12 characters, but for everything, not just for records that begin with PR.

    Is someone able work out where I've gone wrong and assist?

    Please Login or Register  to view this content.
    Last edited by ports83; 10-25-2012 at 10:31 PM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,945

    Re: Help to return only first 12 characters when a cell contains specific text

    not sure whats wrong with your VBA, but if you want a formula, try this...

    =IF(ISERROR(SEARCH("PR",A1,1)),A1,LEFT(A1,12))
    i assumed your data is in A1, adjust as needed
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    09-05-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Help to return only first 12 characters when a cell contains specific text

    I forgot to attach this earlier... this is the current spreadsheet I'm working with.
    Attached Files Attached Files

  4. #4
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Help to return only first 12 characters when a cell contains specific text

    @ FDibbins,

    Works perfect for me! Better tan VBA.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,945

    Re: Help to return only first 12 characters when a cell contains specific text

    thanks Winon, sometimes simpler is better

  6. #6
    Registered User
    Join Date
    09-05-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Help to return only first 12 characters when a cell contains specific text

    This is actually one part of a larger code to try and automate the file from when I receive it to have it formatted to suit our system and then email out. How do I go about using your formula in VBA so I can add it to the rest of my code?

    My working knowledge of VBA is pretty limited, everything I have written is based off of prior examples and stitched together...

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Help to return only first 12 characters when a cell contains specific text

    Give this a try

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  8. #8
    Registered User
    Join Date
    09-05-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Help to return only first 12 characters when a cell contains specific text

    Mike,
    That seems to give me exactly what I want, though I'm not completly sure what it is doing... but thats my limited knowledge there.
    Thanks heaps!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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