Replacing Bracketed Text In String Using Wildcard Characters in VBA

Wookiee

Active Member
Joined
Nov 27, 2012
Messages
429
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello, MrExcel Community! I have a question which I have so far been unable to answer by googling. I hope you can help.

I have VBA code which is supposed to clean up some undesirable elements from text strings. In some cases, there will be a notation between brackets in the text strings and I would like to remove the entire bracketed segment of the string, but I have been unable to do so.

To illustrate, a couple of examples would be:
"Darth Vader [Empire]" becomes simply "Darth Vader"
"Han Solo [Rebellion]" converts to "Han Solo"

I even tried recording a macro where I used a wildcard in my Search/Replace. Although the recorded code doesn't seem significantly different from what I had already written, my code doesn't strip off the bracketed text.

Recorded Macro
Code:
Selection.Replace What:="
[*]", Replacement:=""

My Code
Code:
strSource = Replace(strSource, "
[*]", "")

I'm assuming that the difference in the functionality might have to do with the fact that the recorded macro worked on cells within a worksheet, whereas my code is attempting to manipulate a string which exists solely in the VBA environment. Can anyone point out what I'm missing or provide an alternate method?

Thanks, all!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this for strings in column "A".
This code should cater for multiple "[***]" in each row.
Code:
[COLOR="Navy"]Sub[/COLOR] MG17Jun36
[COLOR="Navy"]Dim[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range, Rng [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] str [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] fd [COLOR="Navy"]As[/COLOR] Boolean
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    fd = True
    str = ""
    [COLOR="Navy"]For[/COLOR] n = 1 To Len(Dn.Value)
        [COLOR="Navy"]If[/COLOR] Dn.Characters(n, 1).Text = "[" [COLOR="Navy"]Then[/COLOR] fd = False
            [COLOR="Navy"]If[/COLOR] fd [COLOR="Navy"]Then[/COLOR]
                str = str & Dn.Characters(n, 1).Text
            [COLOR="Navy"]End[/COLOR] If
         [COLOR="Navy"]If[/COLOR] Dn.Characters(n, 1).Text = "]" [COLOR="Navy"]Then[/COLOR] fd = True
    [COLOR="Navy"]Next[/COLOR] n
Dn.Value = str
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks, MickG! Although I had hoped to avoid having to loop through each character in the string, your code does accomplish what I wanted to do. I really appreciate your assistance.

While I was awaiting a reply, I continued trying different ways to approach the problem. In the case of the strings I'll be reviewing, the only times bracketed notations should appear is at the end of the string. So I decided to scan the string for an opening bracket, then scrub all the extraneous text off from that point. Here is what I came up with:

Rich (BB code):
Function ScrubTheNaughtyBits _
    (strSource As String)
'Removes Unwanted Characters And Bracketed Notes From String
'Crafted 17 Jun 2016 by Jason B White


Dim bytBracket As Byte

strSource = Replace(strSource, "'", "")
strSource = Replace(strSource, "/", " ")
strSource = Replace(strSource, "&", " ")

'My Original (Non-Working) Code:
'strSource = Replace(strSource, "
[*]", "")

'Revised (Working) Code:
bytBracket = InStr(strSource, "[")
If bytBracket > 0 Then
   strSource = Trim(Left(strSource, bytBracket - 1))
End If

ScrubTheNaughtyBits = strSource


End Function

Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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