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.
 
If you want to delete the original data, have you tried the macro that Eric posted?
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
If you want to delete the original data, have you tried the macro that Eric posted?
Unfortunately,

If Len(w(u)) = 2 Then

was "out of range". I toggled with it a little bit, but I am very new to VBA so I didn't find a solution yet.
 
Upvote 0
I came up with almost the same macro as Eric by coincidence:)
Not completely the same though

VBA Code:
Sub jec()
 Dim ar, a, y, i As Long
 ar = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 2)
 
 For i = 1 To UBound(ar)
   a = Split(ar(i, 1), "*")
   y = UBound(a)
   If IsNumeric(a(y)) Then
     ar(i, 1) = a(y - 1)
     ar(i, 2) = a(y)
   Else
     ar(i, 1) = a(y)
   End If
 Next
  
 Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 2) = ar
End Sub
 
Upvote 0
I came up with almost the same macro as Eric by coincidence:)
Not completely the same though

VBA Code:
Sub jec()
 Dim ar, a, y, i As Long
 ar = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 2)
 
 For i = 1 To UBound(ar)
   a = Split(ar(i, 1), "*")
   y = UBound(a)
   If IsNumeric(a(y)) Then
     ar(i, 1) = a(y - 1)
     ar(i, 2) = a(y)
   Else
     ar(i, 1) = a(y)
   End If
 Next
 
 Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 2) = ar
End Sub
This one runs! but now it is deleting the name from the original cell. You are left with only two cells (the ID and age), rather than 3 separate cells. I'm sorry I don't think I explained it well.
 
Upvote 0
Ok, and now?


VBA Code:
Sub jec()
 Dim ar, a, y, i As Long
 ar = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3)
 
 For i = 1 To UBound(ar)
   a = Split(ar(i, 1), "*")
   y = UBound(a)
   If IsNumeric(a(y)) Then
     ar(i, 2) = a(y - 1)
     ar(i, 3) = a(y)
   Else
     ar(i, 2) = a(y)
   End If
 Next
   
 Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3) = ar
End Sub
 
Upvote 0
Ok, and now?


VBA Code:
Sub jec()
 Dim ar, a, y, i As Long
 ar = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3)
 
 For i = 1 To UBound(ar)
   a = Split(ar(i, 1), "*")
   y = UBound(a)
   If IsNumeric(a(y)) Then
     ar(i, 2) = a(y - 1)
     ar(i, 3) = a(y)
   Else
     ar(i, 2) = a(y)
   End If
 Next
  
 Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3) = ar
End Sub
Yes! This one runs and keeps all the data! Can I add a step to delete the ID and age from the name cell? So the original is now just the name.

I attached an image: the first row is what the macro result is, the second row is the objective in theory.
 

Attachments

  • ONLINE.png
    ONLINE.png
    6.6 KB · Views: 4
Upvote 0
Ok how about

VBA Code:
Sub jec()
 Dim ar, a, y, i As Long
 ar = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3)
 
 For i = 1 To UBound(ar)
   a = Split(ar(i, 1), "*")
   y = UBound(a)
   If IsNumeric(a(y)) Then
     ar(i, 2) = a(y - 1)
     ar(i, 3) = a(y)
     ar(i, 1) = Join(Filter(Filter(a, a(y - 1), 0), a(y), 0))
   Else
     ar(i, 2) = a(y)
     ar(i, 1) = Join(Filter(a, a(y), 0))
   End If
 Next
   
 Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3) = ar
End Sub
 
Upvote 0
Solution
Ok how about

VBA Code:
Sub jec()
 Dim ar, a, y, i As Long
 ar = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3)
 
 For i = 1 To UBound(ar)
   a = Split(ar(i, 1), "*")
   y = UBound(a)
   If IsNumeric(a(y)) Then
     ar(i, 2) = a(y - 1)
     ar(i, 3) = a(y)
     ar(i, 1) = Join(Filter(Filter(a, a(y - 1), 0), a(y), 0))
   Else
     ar(i, 2) = a(y)
     ar(i, 1) = Join(Filter(a, a(y), 0))
   End If
 Next
  
 Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3) = ar
End Sub
YES! This is exactly it, thank you for your patience! This is my first time using VBA and (honestly Excel for anything more than a basic table :) So you can say I am definitely learning. I really appreciate your help.
 
Upvote 0
You're welcome! Good luck learning ;)
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

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