Is there a VBA code using REGEX to help me delimit the 2 rightmost "*" under certain conditions?

EmilyCrandall

New Member
Joined
May 17, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Emilia*Grace Johnson*IT0037201X*23
Megan*2 Richards*PE018187
Isabel Sneider*TEC017106S*43
Isaiah Lee*TEC017102X
Billy*Bob*3 Joel*TBF016052H*35


Is there a VBA Code using REGEX to delimit the employee ID and age at the end of the line using the "*"?

Some of my rows have up to 4 "*" (used for hyphenated names and/or the amount of positions worked) but I only want the employee ID and age to be delimited. Some rows don't have an age either.

(If it helps, the ages will always only be 2 digits)

Any ideas would help greatly.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Welcome to the MrExcel forum!

I don't know if you have some particular reason for using REGEX, but there are many ways to accomplish that. For example:

VBA Code:
Sub Test2()

    d = Range("A2:A6").Value
    ReDim res(1 To UBound(d), 1 To 2)
    
    For i = 1 To UBound(d)
        w = Split(d(i, 1), "*")
        u = UBound(w)
        If Len(w(u)) = 2 Then
            res(i, 1) = w(u - 1)
            res(i, 2) = w(u)
        Else
            res(i, 1) = w(u)
        End If
    Next i
    
    Range("C2:D6") = res
            
End Sub

This takes the values in A2:A6 and returns the values in C:D.

Book1
ABCD
1IDAge
2Emilia*Grace Johnson*IT0037201X*23IT0037201X23
3Megan*2 Richards*PE018187PE018187
4Isabel Sneider*TEC017106S*43TEC017106S43
5Isaiah Lee*TEC017102XTEC017102X
6Billy*Bob*3 Joel*TBF016052H*35TBF016052H35
Sheet8
 
Upvote 0
Another option with a formula
++Fluff.xlsm
ABC
1
2Emilia*Grace Johnson*IT0037201X*23IT0037201X23
3Megan*2 Richards*PE018187PE018187
4Isabel Sneider*TEC017106S*43TEC017106S43
5Isaiah Lee*TEC017102XTEC017102X
6Billy*Bob*3 Joel*TBF016052H*35TBF016052H35
Main
Cell Formulas
RangeFormula
B2:C2,B6:C6,B4:C4,B3,B5B2=LET(f,FILTERXML("<k><m>"&SUBSTITUTE(A2,"*","</m><m>")&"</m></k>","//m[position()>=last()-1]"),IF(COUNT(f),TRANSPOSE(f),INDEX(f,2)))
Dynamic array formulas.
 
Upvote 0
Another option with a formula
++Fluff.xlsm
ABC
1
2Emilia*Grace Johnson*IT0037201X*23IT0037201X23
3Megan*2 Richards*PE018187PE018187
4Isabel Sneider*TEC017106S*43TEC017106S43
5Isaiah Lee*TEC017102XTEC017102X
6Billy*Bob*3 Joel*TBF016052H*35TBF016052H35
Main
Cell Formulas
RangeFormula
B2:C2,B6:C6,B4:C4,B3,B5B2=LET(f,FILTERXML("<k><m>"&SUBSTITUTE(A2,"*","</m><m>")&"</m></k>","//m[position()>=last()-1]"),IF(COUNT(f),TRANSPOSE(f),INDEX(f,2)))
Dynamic array formulas.
Hey Fluff!!
This worked for my table of about 80 rows, the sheet I am applying this to uses project names, not people names (due to confidentiality reasons, I changed them). So a few project names have "&", but on the rows with "&" within the name, the formula came with the error "VALUE".
(There are no "&" within the ID or age- just the names)
 
Upvote 0
Ok, try
Excel Formula:
=LET(f,FILTERXML("<k><m>"&SUBSTITUTE(SUBSTITUTE(A2,"*","</m><m>"),"&","&amp;")&"</m></k>","//m[position()>=last()-1]"),IF(COUNT(f),TRANSPOSE(f),INDEX(f,2)))
 
Upvote 0
Ok, try
Excel Formula:
=LET(f,FILTERXML("<k><m>"&SUBSTITUTE(SUBSTITUTE(A2,"*","</m><m>"),"&","&amp;")&"</m></k>","//m[position()>=last()-1]"),IF(COUNT(f),TRANSPOSE(f),INDEX(f,2)))
Thank you so much!!!! It worked!! I hope you have a fantastic day!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Ok, try
Excel Formula:
=LET(f,FILTERXML("<k><m>"&SUBSTITUTE(SUBSTITUTE(A2,"*","</m><m>"),"&","&amp;")&"</m></k>","//m[position()>=last()-1]"),IF(COUNT(f),TRANSPOSE(f),INDEX(f,2)))
Is there a way I can delete the delimited data from the original cell, and just keep the two new columns?
 
Upvote 0
Only if you convert the formula to values.
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

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