+ Reply to Thread
Results 1 to 6 of 6

Excel formula with finding last set of characters

  1. #1
    Registered User
    Join Date
    05-27-2013
    Location
    BEDS
    MS-Off Ver
    Excel 2003
    Posts
    44

    Excel formula with finding last set of characters

    Hi all,

    I am currently creating a documentation which will contain some VBA to import data into Cell A and then copy all the formulas down to the bottom of the data set. I have managed to do all the formulas relating to this so that's great except for the main area. I'm not sure how is the best way to do this.

    It's difficult to explain so I'll try my best with examples:

    The filename will always start like this:

    1) c:\test\blablabla\Inbound\1709\1709-567890
    2) c:\test\blablabla\Inbound\1709\1709-567890\
    3) c:\test\blablabla\Inbound\1709\1709-567890\abc\def\ghi

    At the very minimum it will be number 1 or it could be endless of (like number 3)
    The numbers represent Year and Month

    What I am trying to do is look for the \ after YYMM (main folder 1709) and then either of the following:

    1) Look for the next \ and give me all the numbers in between the 2 \ (Example 2 will give me 1709-567890)

    I do have a formula but this only works for Example 2, the formula is the length of the filename - the last \ will give me the results in between. However this will not work for 1 and 3.

    Does anyone have any suggestion on what would be the best formula to use

    Here is the formula for example 2 if you think this is a good way of doing this:

    Please Login or Register  to view this content.
    Hope this helps

    Thanks

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Excel formula with finding last set of characters

    Is this portion "1709-567890", always of fixed length? And portion before it?

    It would help if you could give realistic variation that can occur.

    Other than that, if structure is always like 1 to 3...
    =MID(A1,32,11)
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    05-27-2013
    Location
    BEDS
    MS-Off Ver
    Excel 2003
    Posts
    44

    Re: Excel formula with finding last set of characters

    Thanks CK76 for the advise...

    So the portion "1709-567890" is a user entered field... What the spreadsheet is doing is looking at folders being created on the system.. those numbers are being picked up by the VBA code and imported as it is into the excel sheet.

    So the ideal scenario is that they should all be 11 characters, but user error could exceed or enter to little of the filename if they enter it incorrectly. I tried using the MID formula but for the above is the reason is why I cant.

    The portion before currently is always "\\test\blablabla\Inbound\1709\1709-567890" unless it changes - it is on a shared network, so unless they change the process (which I doubt) it will remain that way.

    So as you can see the variation could be anything from below:

    \\test\blablabla\Inbound\1709\1709-567890
    \\test\blablabla\Inbound\1709\1709-567890\
    \\test\blablabla\Inbound\1709\1709-567890\abc
    \\test\blablabla\Inbound\1709\1709-567890\abc\def
    \\test\blablabla\Inbound\1709\1709-5678901111
    \\test\blablabla\Inbound\1709\1709-56789011\
    \\test\blablabla\Inbound\1709\1709-5679

    so on and so forth...
    Last edited by pasqui83; 11-23-2017 at 05:30 PM.

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Excel formula with finding last set of characters

    Ok, let's do it in stages then.
    1. To count how many "\" is in the string...
    =LEN(A2)-LEN(SUBSTITUTE(A2,"\",""))

    2. You are always interested in string that's after r 6th "\" to find 6th "\"'s position.
    =FIND("|",SUBSTITUTE(A2,"\","|",6))

    3. To extract everything after 6th "\".
    =MID(A2,FIND("|",SUBSTITUTE(A2,"\","|",6))+1,50)

    Above is enough for those with only 6 "\" in string, but you need additional logic for those with more than 6 "\"

    Basically, you need to find 7th position of "\" and subtract 6th position of "\" from it.

    Combining everything, formula would be something like...
    Please Login or Register  to view this content.
    I'd recommend splitting steps into helper cell to make it easier to follow.

  5. #5
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Excel formula with finding last set of characters

    Try this one
    Enter in B2 and copy down
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    v A B
    1
    2 \\test\blablabla\Inbound\1709\1709-567890 1709-567890
    3 \\test\blablabla\Inbound\1709\1709-567890\ 1709-567890
    4 \\test\blablabla\Inbound\1709\1709-567890\abc 1709-567890
    5 \\test\blablabla\Inbound\1709\1709-567890\abc\def 1709-567890
    6 \\test\blablabla\Inbound\1709\1709-5678901111 1709-5678901111
    7 \\test\blablabla\Inbound\1709\1709-56789011\ 1709-56789011
    8 \\test\blablabla\Inbound\1709\1709-5679 1709-5679
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  6. #6
    Registered User
    Join Date
    05-27-2013
    Location
    BEDS
    MS-Off Ver
    Excel 2003
    Posts
    44

    Re: Excel formula with finding last set of characters

    Thank you Alkey, I tried the formula as it is but this did not work at all, I wasn't sure how the formula works at all i'm afraid to rectify this.

    CK76 - That worked a treat... I did create my own formula prior to see yours which was extremely complex. It was to check for the word "bound\"+6, if it errors, then return blank, then repeat the process again but to find the next bracket, if this errors, then sum the length of my string minus my last \. This would have got me my 4 digit number. I repeated the process to extract the 11 digit number. It looks something like this:


    Extracting the 4 digit number:
    I first extracted the word Inbound / Outbound into B2, if this does not exist then it will return me blank


    Please Login or Register  to view this content.
    Extracting the next set of numbers (if any)

    Please Login or Register  to view this content.

    After playing about with the substitute, I've managed to make it even more efficient and easier to read (although I have saved the one above for future reference for myself). I am now using the real data set, where it for the first 4 digits, it would be after the 8th \ occurrence.

    Extracting the 4 digit code using substitute
    If the 8th \ errors, return me no results, if there are only 8 \ then return me the last row of data at the end of the dataset otherwise look for the 8th and 9th bracket and give me everything in between the 2 brackets.

    Please Login or Register  to view this content.
    The same was applied to the 11 digit incident number... but looking for the 9th and 10th occurrence of the \

    Please Login or Register  to view this content.
    Thanks again for the help. I have only ever used substitute once previously but never really understood how it works.. I now need to make it dynamic so I will add this to the parameter page so only certain users can make the changes if required.
    Last edited by pasqui83; 11-26-2017 at 04:24 AM.

+ 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] Macro not finding finding selected characters in strings
    By Ochimus in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-12-2017, 01:47 AM
  2. Finding special characters in Excel 2010
    By jorguev in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-28-2012, 03:10 PM
  3. Finding special characters in Excel 2010
    By excelforthemasses in forum Excel General
    Replies: 3
    Last Post: 12-28-2012, 01:50 PM
  4. Finding Duplicate Characters
    By mballard5574 in forum Excel General
    Replies: 3
    Last Post: 07-28-2011, 12:37 PM
  5. Formula for finding Special characters
    By Cullen8 in forum Excel General
    Replies: 11
    Last Post: 03-18-2010, 03:49 AM
  6. Finding case of characters
    By ketedford in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-28-2007, 02:21 PM
  7. Replies: 4
    Last Post: 04-17-2006, 06:45 AM

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