How can I parse a string from a cell multiple time in excel?

Centimane

I have a column in my datasheet that often contains a string that I want to place in another column. This string may occur multiple times and I want to place ALL instances of it into the other column. The column is delimited and I'd like to take the matching string and up to the delimiter.

An example:

Possessions
Fruit: apple, Car: Ford, Fruit: banana,
Car: Saturn,
Fruit: orange,

I'd like the next column to contain:

Fruit
Fruit: apple, Fruit: banana,

Fruit: orange,

It's easy enough to find the first instance of the string (new lines are for readability):

MID(A2, 
    FIND( *first instance of Fruit:* ), 
    FIND( *first comma after Fruit:* ) - FIND( *first instance of Fruit:* )
    )

However I could encounter the string any number of times and want to catch all of them.

Also, the column is already a calculated field (a reference to another sheet) so I cannot use text to columns to split on the delimiter.

Any ideas on how to return all instances of the string? I'd rather avoid a VBA script if possible and use worksheet functions, but if it's not possible with functions I'm open to VBA.

nixda

Generic VBA solution with regular expressions

This method should cover your needs. It can also be used by other users to extract multiple strings from a given string with the help of regular expressions

enter image description here

  1. Open your VBA editor (ALT+F11)
  2. Insert a new module (!) and paste the below code into it
  3. Go back to Excel and use this formula in a cell where you want your output

    =REGEXTRACT(A1, "Fruit: .*?,")
    

Formula explanation

  • =REGEXTRACT() is your new custom formula.
  • A1 is the cell where your input data is placed
  • Fruit: .*?, is a regular expression to find all occurrence of fruit and matches until the very next comma.
Function REGEXTRACT(objCell As Range, strPattern As String)

    Dim objMatches As Object
    Dim RegEx As Object
    Set RegEx = CreateObject("VBScript.RegExp")

    RegEx.IgnoreCase = True
    RegEx.Global = True
    RegEx.Pattern = strPattern

    Set objMatches = RegEx.Execute(objCell.Value)

    If objMatches.Count <> 0 Then
        For Each objMatch In objMatches
            REGEXTRACT= REGEXTRACT+ objMatch.Value
        Next objMatch
    Else: REGEXTRACT= ""
    End If

End Function

Hint: Look-Behind and Look-Ahead expressions are not supported under VB's regex engine. So it's not trivial to exclude the comma via RegEx. But it's possible via normal VBA string operations.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I pass multiple cell values of datagridview from one form to another at same time in C#?

From Dev

How can I parse out the dimensions for this string in Excel?

From Dev

How can I link excel cell to updated value from web?

From Dev

How can I parse out this string from a longer string?

From Dev

How can I parse the Euro symbol from a string?

From Dev

How can I parse only the float number from the string?

From Dev

How can I detect and parse from one of these string formats in JavaScript?

From Dev

How can I parse this json string from an AJAX success function?

From Dev

How can I parse specific text from a string?

From Dev

How can I reformat a time string in multiple files?

From Dev

How do i remove date from a date and time from an excel cell

From Dev

How can I extract time information from excel

From Dev

Parse Data From Excel Cell

From Dev

How can I query data from multiple tables and sort by time?

From Dev

How can I query data from multiple tables and sort by time?

From Dev

How can I get data from multiple Siemens PLCs into Excel?

From Dev

How can I extract data from multiple files in a folder of excel

From Dev

How can i count same text cell values count for multiple values in excel sheet

From Dev

How can I get only the date and time from the string?

From Dev

How can i parse JSON with multiple arrays?

From Dev

How can I extract specified values from a string in excel document?

From Dev

How can I modify (change to bold) a specific string of text in a merged cell in Microsoft Excel VBA?

From Dev

How can I parse parse date with time and miliseconds in Python?

From Dev

how can I parse string "2015.02.21 1:00 AM GMT" to local date time?

From Dev

How can I parse string into Hiccup?

From Dev

How can I parse this datetime string?

From Dev

How can I parse a dictionary string?

From Dev

How can I parse the following string into DateTime?

From Dev

How can I parse this datetime string?

Related Related

  1. 1

    How can I pass multiple cell values of datagridview from one form to another at same time in C#?

  2. 2

    How can I parse out the dimensions for this string in Excel?

  3. 3

    How can I link excel cell to updated value from web?

  4. 4

    How can I parse out this string from a longer string?

  5. 5

    How can I parse the Euro symbol from a string?

  6. 6

    How can I parse only the float number from the string?

  7. 7

    How can I detect and parse from one of these string formats in JavaScript?

  8. 8

    How can I parse this json string from an AJAX success function?

  9. 9

    How can I parse specific text from a string?

  10. 10

    How can I reformat a time string in multiple files?

  11. 11

    How do i remove date from a date and time from an excel cell

  12. 12

    How can I extract time information from excel

  13. 13

    Parse Data From Excel Cell

  14. 14

    How can I query data from multiple tables and sort by time?

  15. 15

    How can I query data from multiple tables and sort by time?

  16. 16

    How can I get data from multiple Siemens PLCs into Excel?

  17. 17

    How can I extract data from multiple files in a folder of excel

  18. 18

    How can i count same text cell values count for multiple values in excel sheet

  19. 19

    How can I get only the date and time from the string?

  20. 20

    How can i parse JSON with multiple arrays?

  21. 21

    How can I extract specified values from a string in excel document?

  22. 22

    How can I modify (change to bold) a specific string of text in a merged cell in Microsoft Excel VBA?

  23. 23

    How can I parse parse date with time and miliseconds in Python?

  24. 24

    how can I parse string "2015.02.21 1:00 AM GMT" to local date time?

  25. 25

    How can I parse string into Hiccup?

  26. 26

    How can I parse this datetime string?

  27. 27

    How can I parse a dictionary string?

  28. 28

    How can I parse the following string into DateTime?

  29. 29

    How can I parse this datetime string?

HotTag

Archive