VBA find "text" and replace adjacent characters

em-digity

New Member
Joined
Nov 8, 2011
Messages
5
Hey gurus,

Complete newb here. I am trying to build a macro for removing/replacing text from a .log file using VBA. I know how to set up a find/replace macro, but I need to look for something like "Lot: aaaa, Recipe: bbbb, Layer: cccc" where the aaaa, bbbb, cccc are unknown names of unknown lengths.

In this case I would like to replace "aaaa" "bbbb" and "cccc" with "IP Scrubbed" to result in: "Lot: IP Scrubbed, Recipe: IP Scrubbed, Layer: IP Scrubbed"

I don't know how to find for example "Lot:" and replace the adjacent text "aaaa" (which may be up to 25 characters) with "IP Scrubbed"

Is this possible with VBA or am I taking to wrong approach all together?

Thanks in advance.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
If the search text will never appear in the titles, perhaps this:

Code:
Sub test()
Dim t, strTest, u As Long, v
strTest = "Lot: aaaa, Recipe: bbbb, Layer: cccc"
v = Array("aaaa", "bbbb", "cccc")
For u = LBound(v) To UBound(v)
    strTest = Replace(strTest, v(u), "IP SCRUBBED")
Next
MsgBox strTest
End Sub
 
Last edited:
Upvote 0
Thanks guys. Problem is that the "aaaa" "bbbb" or "cccc" are RANDOM values. They will be different every time. So I need a way to search for a known, i.e. "Lot:" or "Layer:" or "Recipe:" and then replace the following random characters up to and not including the next comma with "IP Scrubbed" :confused:
 
Upvote 0
Are Lot:, Layer: and Recipe: consistent values in the string?
 
Upvote 0
Code:
Sub test()
Dim t, strTest, u As Long, v
strTest = "Lot: aaaa, Recipe: bbbb, Layer: cccc"
t = Array("Lot: ", "Layer: ", "Recipe: ")
v = Split(strTest, ",")
For u = LBound(v) To UBound(v)
    v(u) = t(u) & "IP SCRUBBED"
Next
strTest = Join(v, ",")
MsgBox strTest
End Sub
 
Upvote 0
Code:
Sub test()
Dim t, strTest, u As Long, v
strTest = "Lot: aaaa, Recipe: bbbb, Layer: cccc"
t = Array("Lot: ", "Layer: ", "Recipe: ")
v = Split(strTest, ",")
For u = LBound(v) To UBound(v)
    v(u) = t(u) & "IP SCRUBBED"
Next
strTest = Join(v, ",")
MsgBox strTest
End Sub

Hmmmm, maybe I'm doing something wrong. That didn't work for me. I do see that what I was calling "Recipe" is actually "Recipe Name" but that's not the issue. Here's part of the log file I'd like to scrub:

"Nov 08,00:37:20.593 INFO [Recipes@serverlocalhost] Recipe Loaded Header. Database:Local, Lot:4573Ahr23, Product:Y54A, Layer:3TRO, Recipe Name:X54A_TEV_1A, Class:, Version:"
 
Last edited:
Upvote 0
Code:
Sub test()
Dim strTest As String, v As Long, w As Long, o, z As Long
strTest = "Nov 08,00:37:20.593 INFO [Recipes@serverlocalhost] Recipe Loaded Header. Database:Local, Lot:4573Ahr23, Product:Y54A, Layer:3TRO, Recipe Name:X54A_TEV_1A, Class:, Version:"
o = Array("Lot:", "Layer:", "Recipe Name:")
For z = LBound(o) To UBound(o)
    v = InStr(strTest, o(z))
    If v Then
        w = Len(Split(Mid(strTest, v), ",")(0))
        strTest = Replace(strTest, Mid(strTest, v, w), o(z) & " IP Scrubbbed")
    End If
Next
MsgBox strTest
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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