+ Reply to Thread
Results 1 to 4 of 4

Extract a list of codes from data

Hybrid View

  1. #1
    Registered User
    Join Date
    04-08-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    85

    Extract a list of codes from data

    I've got data such as the following:

    Resources Nl Materials0.0000ACSC:\Stocks\All Acrux Limited Pharmaceuticals0.0000ACRC:\Stocks\All Adamus Resources Limited Materials-2.5317ADUC:\Stocks\All Adelaide Brighton Limited Materials2.4242ABCC:\Stocks\All Aditya Birla Minerals Limited Materials1.1236ABYC:\Stocks\All Advanced Braking Technolo Automobile & Co3.5714ABVC:\Stocks\All Advanced....

    and I want to extract a list of the three-letter alphabetic codes which appear directly to the left of every 'C:\' symbol - anyone know a formula or VBA code which could do this please? Thanks in advance.
    Last edited by jp001; 03-06-2011 at 04:02 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Extract a list of codes from data

    Assuming the string in cell A1...

    1st code: =MID(A1, FIND("C:\", A1)-3, 3)
    2nd code: =MID(A1, FIND("||", SUBSTITUTE(A1,"C:\","||",2))-3,3)
    3rd code: =MID(A1, FIND("||", SUBSTITUTE(A1,"C:\","||",3))-3,3)
    _________________
    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!)

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Extract a list of codes from data

    If on the off chance you wanted a single delimited list then you could use a User Defined Function, eg:

    Function GetCodes(rngText As Range, Optional strDelim As String = " ") As String
        Const cDelim = "C:\"
        Dim vData, vCodes, lngR As Long, lngC As Long, lngCode As Long
        If rngText.Cells.Count = 1 Then
            ReDim vData(1 To 1, 1 To 1)
            vData(1, 1) = rngText.Value
        Else
            vData = rngText.Value
        End If
        For lngC = LBound(vData, 2) To UBound(vData, 2) Step 1
            For lngR = LBound(vData, 1) To UBound(vData, 1) Step 1
                vCodes = Split(Application.Trim(vData(lngR, lngC)), cDelim)
                For lngCode = LBound(vCodes) To UBound(vCodes) - 1 Step 1
                    GetCodes = GetCodes & strDelim & Right(vCodes(lngCode), 3)
                Next lngCode
            Next lngR
        Next lngC
        GetCodes = Replace(GetCodes, strDelim, "", 1, 1)
    End Function
    the above, stored in a standard module, would be called from a cell along the lines of:

    =GETCODES(A1)
    where A1 holds the string - the range you pass can encompass multiple cells (though it is assumed any multi cell range would be contiguous)

  4. #4
    Registered User
    Join Date
    04-08-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Extract a list of codes from data

    Thanks very much!

+ 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