Consulting

Results 1 to 8 of 8

Thread: Extract data between brackets

  1. #1
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    Unhappy Extract data between brackets

    I need a VBA Code that can extract data from column A between brackets () like the below example :

    I have these data on column A1 and when run VBA code must give me the below result :

    VP,ST,VCAD,X27,(PC3601),PC3601,PC3601_0_1,(TR),(VP),(PS3601)

    Result must be in column B1

    (PC3601),(TR),(VP),(PS3601)

  2. #2
    The following will do it using a macro. It should be possible to use a function also, but I'll leave that to the Excel specialists.

    Dim LastRow As Long
    Dim i As Long
    Dim vCell As Variant
        LastRow = Cells(Rows.Count, "A").End(xlUp).Row
        For i = 1 To LastRow        'Change 1 to 2 if there is a header row
            vCell = Split(Cells(i, 1), Chr(44))
            Cells(i, 2) = vCell(4) & Chr(44) & _
                          vCell(7) & Chr(44) & _
                          vCell(8) & Chr(44) & _
                          vCell(9)
        Next i
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Dear gmayor , Thanks for your help but when run your vba code this error will be appear

    Run-time error '9':

    Sunscript out of range

    I added the Excel file that need this VBA code for it.

    Thank you again
    Attached Files Attached Files

  4. #4
    It doesn't work because your column content bears no relationship to the quoted example. There is no consistency in the column data that would lend itself to such a macro.

    How for example does

    "CANOPY US"

    relate to

    "L = 600 MM"

    or

    "(14014602),L = 35 MM,M8x35,(VOE 955298),(955298),L=35,E PROD,AC: 14256846,L = 35mm: TD 61 GB,L = 35mm,L = 35,L50B,(11993555)"

    I think the task is impossible.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Hi

    You can also try this udf:

    Function ExtractData(s As String) As String
    
    ExtractData = Join(Filter(Split(s, ","), "("), ",")
    End Function

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    lecxe's method works fine. Select column B and change the format to General. Add the macro code to a Module. In B1, =ExtractData(A1), and copy or fill down.

  7. #7
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Thanks, You save me .

  8. #8
    You're welcome. Thanks for the feedback.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •