+ Reply to Thread
Results 1 to 5 of 5

Extract a data pattern (character combo may change but not the pattern) from a cell

Hybrid View

vaidy44 Extract a data pattern... 01-22-2020, 01:52 AM
WideBoyDixon Re: Extract a data pattern... 01-22-2020, 08:19 AM
jindon Re: Extract a data pattern... 01-22-2020, 09:46 AM
WideBoyDixon Re: Extract a data pattern... 01-22-2020, 10:48 AM
vaidy44 Re: Extract a data pattern... 01-27-2020, 04:40 AM
  1. #1
    Registered User
    Join Date
    01-22-2020
    Location
    Bangalore
    MS-Off Ver
    Office 13
    Posts
    2

    Extract a data pattern (character combo may change but not the pattern) from a cell

    In a cell there will be large amount of data. I am looking for a macro that can identify a specific text pattern and extract and paste in a different cell.
    Pattern will be consistent
    characters in the pattern will not be consistent
    There may be space before and after the pattern begins and ends
    In some cases, there may not be space before or after

    Pattern will be like xxxxxx-xxxxxx-xxxxxx-xxxxxx
    It will be a combination of numerics and texts.

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Extract a data pattern (character combo may change but not the pattern) from a cell

    Paste this code into a new module:

    Private regex As Object
    Public Function ExtractPattern(sourceString As String, patternString As String, Optional group As Variant) As String
    
    Dim mc As Object
    
    If regex Is Nothing Then Set regex = CreateObject("VBScript.RegExp")
    With regex
        .IgnoreCase = True
        .Global = True
        .Pattern = patternString
        If .Test(sourceString) Then
            Set mc = .Execute(sourceString)
            If IsMissing(group) Then
                ExtractPattern = mc.Item(0).Value
            Else
                If mc.Item(0).SubMatches.Count < group Then
                    ExtractPattern = mc.Item(0).Value
                Else
                    ExtractPattern = mc.Item(0).SubMatches(group - 1)
                End If
            End If
        Else
            ExtractPattern = ""
        End If
    End With
    
    End Function
    Then you can use, for example, the following formula to extract the pattern:

    =ExtractPattern($A1,"([^0-9a-z]|^)([0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{5})([^0-9a-z]|$)",2)
    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Extract a data pattern (character combo may change but not the pattern) from a cell

    Assuming Data in col.A, output in Col.B
    Sub test()
        Dim a, i As Long, txt As String, m As Object
        With Range("a1", Range("a" & Rows.Count).End(xlUp))
            a = .Value
            With CreateObject("VBScript.RegExp")
                .Global = True
                .Pattern = "[\dA-Za-z]{6}(-[\dA-Za-z]{6}){3}"
                For i = 1 To UBound(a, 1)
                    For Each m In .Execute(a(i, 1))
                        txt = txt & IIf(txt <> "", ", ", "") & m
                    Next
                    a(i, 1) = txt: txt = Empty
                Next
            End With
            .Columns(2).Value = a
        End With
    End Sub

  4. #4
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Extract a data pattern (character combo may change but not the pattern) from a cell

    Arrgh! My eyes are too old; I didn't see there were 6 x's in each group. You *could* therefore use this:

    =ExtractPattern($A1,"([^0-9a-z]|^)([0-9a-z]{6}-[0-9a-z]{6}-[0-9a-z]{6}-[0-9a-z]{6})([^0-9a-z]|$)",2)
    Note that this pattern *insists* that there isn't an extra matching character at the beginning/end of the matched string.

    WBD

  5. #5
    Registered User
    Join Date
    01-22-2020
    Location
    Bangalore
    MS-Off Ver
    Office 13
    Posts
    2

    Re: Extract a data pattern (character combo may change but not the pattern) from a cell

    Thank you so much Dixon. The macro did work. Thanks again.

+ 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. Extract data Using regular pattern
    By Imran/CVT in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 10-30-2019, 01:26 PM
  2. Replies: 7
    Last Post: 08-27-2019, 08:37 AM
  3. [SOLVED] How to extract data from a cell match a certain pattern and PRINT or DISPLAY the out
    By bennyys in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-24-2017, 11:48 AM
  4. [SOLVED] Lookup and return rows based on pattern start and pattern end
    By JDI in forum Excel General
    Replies: 18
    Last Post: 11-16-2014, 11:44 PM
  5. Extract data between match pattern, process in another sheet
    By 123raajesh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-30-2014, 12:38 PM
  6. Copy down hypertext cell range pattern & cell reference formula pattern
    By Underexcelling in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-10-2014, 03:23 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