+ Reply to Thread
Results 1 to 8 of 8

extract partial string from Cell

Hybrid View

sydcoco extract partial string from... 04-27-2013, 07:35 PM
Tinbendr Re: extract partial string... 04-27-2013, 09:57 PM
Leith Ross Re: extract partial string... 04-27-2013, 10:44 PM
sydcoco Re: extract partial string... 04-27-2013, 11:09 PM
xladept Re: extract partial string... 04-27-2013, 11:09 PM
Leith Ross Re: extract partial string... 04-27-2013, 11:18 PM
sydcoco Re: extract partial string... 04-27-2013, 11:29 PM
Leith Ross Re: extract partial string... 04-28-2013, 12:34 AM
  1. #1
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: extract partial string from Cell

    Hello sydcoco,

    Welcome to the Forum!

    This macro will search the text of each cell in column "E" starting in row 1 for the words "Joblist" followed by at least one space, 4 digits with or without a alpha character, and the word "provided". Case is ignored in the search and the cell text can be any length.

    Any matches will have the number copied to the adjacent cell in column "G". You can change the starting cell in column "E" to where you want to start and the result column from "G" to the column you choose.
    Sub ExtractString()
    
        Dim c As Long
        Dim Cell As Range
        Dim FirstCell As Range
        Dim LastCell As Range
        Dim RegExp As Object
        Dim ResultColumn As String
        Dim SearchRng As Range
        Dim Text As String
        
            ResultColumn = "G"
            
            Set FirstCell = Range("E1")
            
                Set LastCell = Cells(Rows.Count, "E").End(xlUp)
                If LastCell.Row < FirstCell.Row Then MgBox "There is no data to search.": Exit Sub
    
                Set SearchRng = Range(FirstCell, LastCell)
                
                c = Cells(1, ResultColumn).Column -  SearchRng.Column
                
                Set RegExp = CreateObject("VBScript.RegExp")
                RegExp.IgnoreCase = True
                RegExp.Pattern = ".*Joblist\s+(\d{4}[a-z]?)\s.+(?=provided).*"
            
                For Each Cell In SearchRng
                    Text = Cell.Value
                    If RegExp.Test(Text) = True Then
                        Cell.Offset(0, c) = RegExp.Replace(Text, "$1")
                    End If
                Next Cell
                    
    End Sub
    Last edited by Leith Ross; 04-27-2013 at 11:20 PM. Reason: Changed Rng.Column to SearchRng.Column
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  2. #2
    Registered User
    Join Date
    04-27-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: extract partial string from Cell

    Hi Leith,

    I am speechless. It worked. I fixed up one typing error for the Message box, and corrected a dimension statement. SearchRng instead of Rng.
    I was trying to solve this for days. Thank you so much.

    SOLVED

+ 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