macro to remove brackets () at the end of name

route217 used Ask the Experts™
Hi Experts,

Need a macro to do the following, to look for a name in column G3, worksheet "role" and if the name has (232232) number at the of a persons name then remove the bracket + the numbers betwwen the brackets....for example

1. Joe Doe(32213) replace with Joe Doe (no space between surname and open bracket.

2. Joe Doe (32322) replace with Joe Doe space after surname

these are the only two cases that can exist.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
TracyVBA Developer

Try this, assuming your data is in column A:

Sub kTest()
With Sheets("role").Range("g3")
    .Replace "(*", ""
End With
End Sub

VBA Developer
Modified for Column G.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Not sure I understand what your trying to do could you be a little clearer?

Why can't you just do a find replace?

Do you need to check that the brackets are at the end of the string, and contain numbers only?

If so then the cell value will need parsing, not a wildcard replace.


Top Expert 2010

Hello broomee9,

For the future, please do not make a comment like:

"Try this, assuming your data is in column A:" and post a sample workbook.

When you do that, nothing in your comment in searchable/indexable in EE's search functions.  It also means
that the only way people can see and evaluate your work is by downloading the file.

By all means post a sample workbook, but please also take the time to either post the code, or at least a high
level description of what the code does.


TracyVBA Developer

Will do.
Top Expert 2010

Per Dave's comment above, if we are only looking for numbers in parentheses, this code should do it:

Sub KillTheParens()

    Dim rng As Range, cel As Range

    With ActiveSheet
        Set rng = Intersect(.UsedRange, .Range("g:g"))
    End With

    For Each cel In rng.Cells
        cel = RegExpReplace(cel, " *\( *\d+ *\) *$", "")

    MsgBox "Done"

End Sub

Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True, _
    Optional MultiLine As Boolean = False)
    ' Function written by Patrick G. Matthews.  You may use and distribute this code freely,
    ' as long as you properly credit and attribute authorship and the URL of where you
    ' found the code
    ' For more info, please see:
    ' This function relies on the VBScript version of Regular Expressions, and thus some of
    ' the functionality available in Perl and/or .Net may not be available.  The full extent
    ' of what functionality will be available on any given computer is based on which version
    ' of the VBScript runtime is installed on that computer
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll
    ' controls whether all instances of the matched string are replaced (True) or just the first
    ' instance (False)
    ' If you need to replace the Nth match, or a range of matches, then use RegExpReplaceRange
    ' instead
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    ' If you use this function from Excel, you may substitute range references for all the arguments
    ' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
    ' where a large number of calls to this function are made, making RegX a static variable that
    ' preserves its state in between calls significantly improves performance
    Static RegX As Object
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
        .MultiLine = MultiLine
    End With
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
End Function

Top Expert 2010

broomee9 said at http:/Q_25093868.html#a0
>>Will do.

Thanks :)



that work just fine......

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial