VBA Pattern matching with replacement of only a portion of pattern found

jfarc

Active Member
Joined
Mar 30, 2007
Messages
316
The goal is to replace all the occurrences of "*** HH:MM:SS " - (3 asterisks followed by a space followed by the time hrs:mins:secs followed by a space) with " ~ HH:MM:SS " (a single tilde followed by a space followed by the same time followed by a space)

The problem is there are also multiple occurrences of the 3asterisks without the time following it that I do not want to replace.

This is an example of data in a single cell:

Code:
  ~ 17:35:28  15 AUG   JENNIFER_HALL ***   ||LIEN-RL| |RESOLVD|    *** 09:11:33  14 AUG   DEALEREXAM ***   ||LIEN-RL| Aug07-(Esta) CUST ORDERED LIEN RELEASE    *** 09:11:23  14 AUG   DEALEREXAM ***   ||IM-FORM| Jul25-(Hall) NEED I.O.A FORM*** 09:11:19  14 AUG   DEALEREXAM ***   ||LIEN-RL| Jul25-(Hall) NEED L/R (PEOPLE'S FIRST LLC) -NO PAYOFF-MD TITLE    IN DEAL-F*I MJ   *** 16:02:20  31 JUL   JUDY,_MARILYN ***   BANK IS MAILING CUSTOMER LIEN RELEASE, WILL TAKE 7 TO 10 DAYS TO RECEIVE,    CUSTOMER WILL BRING IT IN AS SOON AS HE RECEIVES IT. MJJ   *** 09:47:32  22 JUL2014 lindsay JC-FI ESTABROOK,_LINDSAY ***   $49821.78 H/C 7/30   *** 17:36:07  21 JUL   JUDY,_MARILYN ***SENT TO ACCT ON 7/21/14


So, in the above single cell, I want to replace the following 3asterisks and time and a space with 3spaces, tilde and the same time and a space:
Code:
*** 09:11:33 
*** 09:11:23 
*** 09:11:19 
*** 16:02:20 
*** 09:47:32 
*** 17:36:07 
Replace above with:
~ 09:11:33 
~ 09:11:23 
~ 09:11:19 
~ 16:02:20 
~ 09:47:32 
~ 17:36:07

There are (6) other occurrences in this example cell of the 3asterisks that I do not want to replace.

This is what the above single cell would look like after the replacement happens:
Code:
  ~ 17:35:28  15 AUG   JENNIFER_HALL ***   ||LIEN-RL| |RESOLVD|    ~ 09:11:33  14 AUG   DEALEREXAM ***   ||LIEN-RL| Aug07-(Esta) CUST ORDERED LIEN RELEASE    ~ 09:11:23  14 AUG   DEALEREXAM ***   ||IM-FORM| Jul25-(Hall) NEED I.O.A FORM~ 09:11:19  14 AUG   DEALEREXAM ***   ||LIEN-RL| Jul25-(Hall) NEED L/R (PEOPLE'S FIRST LLC) -NO PAYOFF-MD TITLE    IN DEAL-F*I MJ    ~ 16:02:20  31 JUL   JUDY,_MARILYN ***   BANK IS MAILING CUSTOMER LIEN RELEASE, WILL TAKE 7 TO 10 DAYS TO RECEIVE,    CUSTOMER WILL BRING IT IN AS SOON AS HE RECEIVES IT. MJJ    ~ 09:47:32  22 JUL2014 lindsay JC-FI ESTABROOK,_LINDSAY ***   $49821.78 H/C 7/30    ~ 17:36:07  21 JUL   JUDY,_MARILYN ***SENT TO ACCT ON 7/21/14

Solution needs to be in VBA rather than a cell formula. Assume the column of data is in col 'A'. My failed attempted solutions either replaces all asterisks or replaces the original time wiping it out.
 
Using your requested Column A for the data, here is what I came up with...
Code:
Sub ThreeAsterisks()
  Dim R As Long, X As Long, Data As Variant, Asterisks() As String
  Data = Range("A1", Cells(Rows.Count, "A").End(xlUp))
  For R = 1 To UBound(Data)
    Asterisks = Split(Data(R, 1), "***")
    For X = 1 To UBound(Asterisks)
      If IsDate(Split(Asterisks(X))(1)) Then Asterisks(X) = Chr(1) & Asterisks(X)
    Next
    Data(R, 1) = Replace(Join(Asterisks, "***"), "***" & Chr(1), "   ~")
  Next
  Range("A1", Cells(Rows.Count, "A").End(xlUp)) = Data
End Sub
Use ZVI's code in Message #9 instead of the code I posted above... it's much faster. I get his code processing 10000 cells in 0.14 seconds whereas my code took 0.5 seconds.
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
ZVI & Rick, I used both of your solutions from post #9&10 and they work very nicely on several days worth of live data. I will put one into production tomorrow and key an eye on it.

Thank you!
 
Upvote 0
Well maybe you just need to put a little more effort into it. Ha!
Okay, I did... the following is the same number of code lines as my previously posted sub (I tend to like compact code) and is only 0.03 second slower than ZVI's sub (0.17 seconds for 10,000 cells versus 0.14 seconds).
Code:
Sub ThreeAsterisks()
  Dim r As Long, X As Long, Data As Variant
  Data = Range("A1", Cells(Rows.Count, "A").End(xlUp))
  For r = 1 To UBound(Data)
    Do
      X = InStr(X + 1, Data(r, 1), "***")
      If Mid(Data(r, 1), X + 3, 9) Like " ##:##:##" Then Mid(Data(r, 1), X, 3) = "@@@"
    Loop While X > 0
    Data(r, 1) = Replace(Data(r, 1), "@@@", "   ~")
  Next
  Range("A1", Cells(Rows.Count, "A").End(xlUp)) = Data
End Sub
 
Last edited:
Upvote 0
(I tend to like compact code)

Well that's obvious! I get a kick out of accomplishing a task with the least amount of effort (of course, that takes into account the vast amount of expertise acquired over the years used to create the solution). I like it and understand it. I'm still chewing on ZVI's code to make sure I understand what each line is doing.

Thanks to the three of you for your efforts, this is exactly what I was looking for.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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