+ Reply to Thread
Results 1 to 17 of 17

Extract specifically formatted string from a larger string

Hybrid View

  1. #1
    Registered User
    Join Date
    08-19-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2011
    Posts
    20

    Extract specifically formatted string from a larger string

    Hello all,

    I have a column of text/number string cells. I want to copy out a specifically formatted string from these cells: ###x### (i.e. 300x600, 500x250, 600x250).

    They can be located anywhere in the string, they can be preceded by different delimiters (-, _, [a space]). They may or may not have text following it, by either the space, _ or -.

    Because it's not located in any specific part of the string, I can't use Trim Left or Right. I can't do Text to Columns because they are all defined by different delimiters and may have text following it. These numbers aren't the only numbers in the string, but they are the only ones with the ###x### formatting.

    An example of what my column looks like:

    Elantra 2011-300x600
    1055868_AMEXOpen_Units-300x600 Boing Boing
    300x250 House Ad
    9253258_WorldCup2010_300x250
    Sprout300x600_V2
    300 X 600 Ad

    DOes anyone have any suggestions? Hints?

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Extract specifically formatted string from a larger string

    Maybe:

    =MID(A1,SEARCH("???x???",A1,1),7)

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

    Re: Extract specifically formatted string from a larger string

    since the data is not consistent enough (there could be x's with strings, like AMEX)... then perhaps you can try installing a free addin from here: Morefunc and use formula:

    =REGEX.MID(SUBSTITUTE(A1," ",""),"[0-9][0-9][0-9][xX][0-9][0-9][0-9]",,FALSE)

    where A1 contains first string to purge.

    Else somebody may be able to supply a VBA solution using regular expressions.
    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.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Extract specifically formatted string from a larger string

    Good spot

    As a vba function:

    Public Function Extractx(rng As Range) As String
    Dim regEx As Object
    Dim regExM As Object
    
    
     Set regEx = CreateObject("vbscript.regexp")
        With regEx
            .Pattern = "[0-9][0-9][0-9][xX][0-9][0-9][0-9]"
            .MultiLine = False
            .Global = False
            .IgnoreCase = True
        End With
         
        Set regExM = regEx.Execute(rng.Value)
        Extractx = regExM(0)
        
    End Function

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

    Re: Extract specifically formatted string from a larger string

    Hi Kyle and thanks for supplying a VBA alternative for the OP.

    However, it returns #VALUE! error for the last sample in the original post because of spaces around the X.... I used the SUBSITITUTE() function to deal with it.. can you adjust your code accordingly for the OP (and me too )

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Extract specifically formatted string from a larger string

    No problem, I assumed that wouldn't meet the requirements of the OP


    Public Function Extractx(rng As Range) As String
    Dim regEx As Object
    Dim regExM As Object
    
    
     Set regEx = CreateObject("vbscript.regexp")
        With regEx
            .Pattern = "[0-9][0-9][0-9][xX][0-9][0-9][0-9]"
            .MultiLine = False
            .Global = False
            .IgnoreCase = True
        End With
         
        Set regExM = regEx.Execute(Replace(rng.Value, " ", ""))
        Extractx = regExM(0)
        
    End Function

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

    Re: Extract specifically formatted string from a larger string

    You might be right, but now, the OP has a choice...

    and I learned something too... thanks Kyle

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Extract specifically formatted string from a larger string

    No probs , a slightly more flexible solution, allowing Regex string and whether spaces should be removed

    Public Function Extractx(rng As Range, regExPattern As String, NoSpaces As Boolean) As String
    Dim regEx As Object
    Dim regExM As Object
    
    
    
     Set regEx = CreateObject("vbscript.regexp")
        With regEx
            .Pattern = regExPattern
            .MultiLine = False
            .Global = False
            .IgnoreCase = True
        End With
        
        If NoSpaces = True Then
            Set regExM = regEx.Execute(Replace(rng.Value, " ", ""))
        Else
            Set regExM = regEx.Execute(rng.Value)
        End If
        
            Extractx = regExM(0)
    
        
        
    End Function

  9. #9
    Registered User
    Join Date
    08-19-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Extract specifically formatted string from a larger string

    Hi all!

    Thanks immensely for all your suggestions and coding! How would I implement this VBA coding in my excel sheet? Does it require an add-on to my excel programme?

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

    Re: Extract specifically formatted string from a larger string

    Where do you actually establish the regex pattern in that code?

  11. #11
    Registered User
    Join Date
    08-19-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Extract specifically formatted string from a larger string

    Also, is there an alternative to the Morefunc add-on for the following code: =REGEX.MID(SUBSTITUTE(A1," ",""),"[0-9][0-9][0-9][xX][0-9][0-9][0-9]",,FALSE)

    I have Excel 2011 (mac) and Morefunc is for pre-2007

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

    Re: Extract specifically formatted string from a larger string

    I don't think Morefunc is compatible with Mac.. I should have noted that from your profile, sorry....

  13. #13
    Registered User
    Join Date
    08-19-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Extract specifically formatted string from a larger string

    That's ok! I still appreciate it! Maybe if someone could advise how I can implement that VBA code or point me in the right direction please?

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Extract specifically formatted string from a larger string

    @NBVC in the function itself, makes it a little more re-usable

    e.g

    =extractx(A1,"[0-9][0-9][0-9][xX][0-9][0-9][0-9]",TRUE)

    @thb, I'm not sure that my code will work for you since it uses windows scripting and I very much doubt that mac will support it. I'll have a think for an alternative and come back to you

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

    Re: Extract specifically formatted string from a larger string

    Quote Originally Posted by Kyle123 View Post
    @NBVC in the function itself, makes it a little more re-usable

    e.g

    =extractx(A1,"[0-9][0-9][0-9][xX][0-9][0-9][0-9]",TRUE)
    Yes, of course I was too focused on expecting it in the VBA code.

  16. #16
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Extract specifically formatted string from a larger string

    Try this, it might not work in all circumstances and there's probably a more elegant way to do it, but it works for your examples:

    Public Function ExtractMac(rng As Range) As String
    
    Dim intL As Integer
    Dim vars
    
    vars = Split(UCase(Replace(rng.Value, " ", "")), "X")
    
    For intL = LBound(vars) To UBound(vars)
    On Error Resume Next
        If (CInt(Right(vars(intL), 3)) And CInt(Left(vars(intL + 1), 3))) Then
            ExtractMac = CInt(Right(vars(intL), 3)) & "x" & CInt(Left(vars(intL + 1), 3))
        End If
    Next intL
    
    End Function
    @thb to use this right click on a sheet tab > view code.

    In the VBE, right click in the project window - on the left where your sheets are listed, insert > module

    Paste the above in the blank page it gives you.

    You can then use it as a normal function ie. =ExtractMac(A1)
    Last edited by Kyle123; 10-11-2011 at 11:55 AM.

  17. #17
    Registered User
    Join Date
    08-19-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Extract specifically formatted string from a larger string

    That worked brilliantly! And I learnt how to open VBA!

    Now to trying to understand the code...

    Thanks to everyone for all your advice, suggestions and codes. It was incredibly helpful!

+ 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