String Parsing

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
201
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
00000 TEMPLATE.xlsx
ABCD
1Copied EntryExpected Result
2Dog*Brown,White,Black,Dragon*Golden,Green,Red,Black,Panda*Red,Black,GreenDog*Brown,White,BlackDragon*Golden,Green,Red,BlackPanda*Red,Black,Green
Sheet1 (2)

As shown above I would like to parse the cell value for every "*" entry to arrive at the expected result, below is my somewhat working code :)
Number of Pet may go up as many as 8 sets, in the above example we have 3 Pet Sets ie. Dog, Dragon and Panda.
VBA Code:
Sub ListOfAnimals()
    Dim aPets() As String
    Dim iCTR As Long
    aPets = Split(Cells(2, 1), "*")
    For iCTR = LBound(aPets) To UBound(aPets)
        Debug.Print aPets(iCTR)
    Next
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Perhaps you could employ a user-defined function like this?

VBA Code:
Function PetList(s As String, Num As Long) As String
  Dim RX As Object, M As Object
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "([^,]+\*.+?)(\,?)(?=([^,]+\*)|$)"
  Set M = RX.Execute(s)
  If Num <= M.Count Then PetList = M(Num - 1).SubMatches(0)
End Function

airforceone_1.xlsm
ABCDE
1Copied EntryExpected Result
2Dog*Brown,White,Black,Dragon*Golden,Green,Red,Black,Panda*Red,Black,GreenDog*Brown,White,BlackDragon*Golden,Green,Red,BlackPanda*Red,Black,Green 
Sheet1
Cell Formulas
RangeFormula
B2:E2B2=PetList($A2,COLUMNS($B:B))
 
Upvote 0
Solution
According to Logic & VBA basics text functions the classic Filter / Split way :​
VBA Code:
Sub Demo1()
  Const D = ","
    Dim S$(), R&
        S = Split([A2].Text, D)
        R = UBound(S)
    While R
        If Not S(R) Like "*[*]*" Then S(R - 1) = S(R - 1) & D & S(R): S(R) = False
        R = R - 1
    Wend
        S = Filter(S, False, False)
        If UBound(S) > -1 Then [B2].Resize(, UBound(S) + 1).Value2 = S
End Sub
 
Upvote 0
Perhaps you could employ a user-defined function like this?

VBA Code:
Function PetList(s As String, Num As Long) As String
  Dim RX As Object, M As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "([^,]+\*.+?)(\,?)(?=([^,]+\*)|$)"
  Set M = RX.Execute(s)
  If Num <= M.Count Then PetList = M(Num - 1).SubMatches(0)
End Function

airforceone_1.xlsm
ABCDE
1Copied EntryExpected Result
2Dog*Brown,White,Black,Dragon*Golden,Green,Red,Black,Panda*Red,Black,GreenDog*Brown,White,BlackDragon*Golden,Green,Red,BlackPanda*Red,Black,Green 
Sheet1
Cell Formulas
RangeFormula
B2:E2B2=PetList($A2,COLUMNS($B:B))
I can't get seems to run the code?
 

Attachments

  • 2022.03.17 - error.JPG
    2022.03.17 - error.JPG
    47.6 KB · Views: 13
Upvote 0
Perhaps the code went in the wrong place. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot .... and copy across (& down if multiple rows).
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)
 
Upvote 0
According to Logic & VBA basics text functions the classic Filter / Split way :​
VBA Code:
Sub Demo1()
  Const D = ","
    Dim S$(), R&
        S = Split([A2].Text, D)
        R = UBound(S)
    While R
        If Not S(R) Like "*[*]*" Then S(R - 1) = S(R - 1) & D & S(R): S(R) = False
        R = R - 1
    Wend
        S = Filter(S, False, False)
        If UBound(S) > -1 Then [B2].Resize(, UBound(S) + 1).Value2 = S
End Sub
not available for Office 2016 - 2019? :)
 
Upvote 0
Perhaps the code went in the wrong place. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy across (& down if multiple rows).
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)
Already saved it to macro-enabled workbook (*.xlsm)
I hope I implemented it right?
 

Attachments

  • 2022.03.17 - error 02.JPG
    2022.03.17 - error 02.JPG
    30 KB · Views: 12
  • 2022.03.17 - error 03.JPG
    2022.03.17 - error 03.JPG
    90.8 KB · Views: 13
Upvote 0
From those images I cannot see where you pasted the code & I cannot see the actual formula in the column B cells, only what you have typed into column C

In the vba window, double-click on the module where you pasted the code then snip an image that shows the module selected (pale grey) & the code on the right

1647482296510.png



Can you also post the range A1:B2 of your sheet with XL2BB
so we can copy/check your actual data and formula?
 
Upvote 0
From those images I cannot see where you pasted the code & I cannot see the actual formula in the column B cells, only what you have typed into column C

In the vba window, double-click on the module where you pasted the code then snip an image that shows the module selected (pale grey) & the code on the right

View attachment 60271


Can you also post the range A1:B2 of your sheet with XL2BB
so we can copy/check your actual data and formula?
00000 TEMPLATE.xlsm
AB
1EntryFormula
2Dog*Brown,White,Black,Dragon*Golden,Green,Red,Black,Eagle*Red,Black,Green#NAME?
StringParsing (2)
Cell Formulas
RangeFormula
B2B2=PetList($A2,COLUMNS($B:B))
 

Attachments

  • 2022.03.17 - E 04.JPG
    2022.03.17 - E 04.JPG
    36.5 KB · Views: 13
Upvote 0
You have placed the code in a different location to that suggested. Try it, as indicated, in a module in the workbook where it will be used, not in your personal macro workbook.
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,708
Members
452,994
Latest member
Janick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top