+ Reply to Thread
Results 1 to 17 of 17

Extract the first 5 characters in a string, and convert them to a number?

  1. #1
    Registered User
    Join Date
    11-07-2008
    Location
    Planet Earth
    Posts
    8

    Extract the first 5 characters in a string, and convert them to a number?

    Say I have a string "09800EBHR052708"

    How would I take the first 5 characters and get a number out of them?

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Try:

    =Left(A1,5)+0

    where A1 contains original string.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    If that string were in cell A5, then:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Quote Originally Posted by NBVC View Post
    Try:

    =Left(A1,5)+0

    where A1 contains original string.
    What does the +0 provide? Also, don't forget the CODE tags, I got infracted for that once already.

  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    The +0 converts a text string (which the Left function extracts) into an actual number.

  6. #6
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127
    Quote Originally Posted by JBeaucaire View Post
    What does the +0 provide? Also, don't forget the CODE tags, I got infracted for that once already.
    You need the +0 to convert the String to a number. If you just use the formula, the result is actually treated as text and can lead to problems in data processing.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello no48,

    Welcome to the Forum!

    Are you looking for a formula or VBA solution to this problem? This forum is for VBA programming. In the future, if you need a formula solution then you should post your question in the Worksheet Functions forum.

    Sincerely,
    Leith Ross

  8. #8
    Registered User
    Join Date
    11-07-2008
    Location
    Planet Earth
    Posts
    8
    Quote Originally Posted by Leith Ross View Post
    Hello no48,

    Welcome to the Forum!

    Are you looking for a formula or VBA solution to this problem? This forum is for VBA programming. In the future, if you need a formula solution then you should post your question in the Worksheet Functions forum.

    Sincerely,
    Leith Ross

    Hi Leith;

    I am looking for a VBA solution for it, I have got an array full of file names, and need to place links to the respective file in the box next to the one corresponding with said file.

  9. #9
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Quote Originally Posted by Leith Ross View Post
    Hello no48,

    Welcome to the Forum!

    Are you looking for a formula or VBA solution to this problem? This forum is for VBA programming. In the future, if you need a formula solution then you should post your question in the Worksheet Functions forum.

    Sincerely,
    Leith Ross
    Maybe I just assumed the OP picked the wrong forum...

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello no48,

    It appears you are using this number as part of a file name. Can you give us an example of what the completed string should look like?

    Sincerely,
    Leith Ross

  11. #11
    Registered User
    Join Date
    11-07-2008
    Location
    Planet Earth
    Posts
    8
    Well, the file name would be 09800EBHR052708-001.jpg

    From that I would want to get a numerical value of 9800 (ie. take the first 5 characters and convert to an integer, or any usable number value)

    If the file were 79200EBHR052708-003.jpg I would want a numerical value of 79200, etc...

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello no48,

    This macro will check the cells in column "A" on the active sheet and place the 5 digit number in the cell to the right on the same row (column "B"). The VBA Val will convert a string up to the first non numeric convertible character. This code will work fine as long as your jpg file names all begin with a number.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  13. #13
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Wouldn't this simple formula filled down to the bottom of the list have been easier?

    =Left(A1,5)+0

  14. #14
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127
    A quick note:

    I think Leith forgot to add one line of code, so it won't work. Amend his code to something like:

    Please Login or Register  to view this content.
    It seems that he had forgotten the With / End With lines

  15. #15
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello BigBas,

    Thanks for catching that.

    Sincerely,
    Leith Ross

  16. #16
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582
    I tried BigBas code and it didn't give me the correct values. I attached my spreadsheet. I copied and pasted direct. Is it because I am on 2000?
    Attached Files Attached Files
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  17. #17
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127
    Chemist:

    Good catch. In correcting Leith's typo, I made one of my own. Try this code:

    Please Login or Register  to view this content.
    Note: We previously had rng.offset and it should have been cell.offset

+ Reply to Thread

LinkBacks (?)

  1. 10-04-2013, 06:33 AM

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