Solved

Data Cleansing & Text Formulas with Excel 2010

Posted on 2012-03-12
6
357 Views
Last Modified: 2012-03-12
Hello Experts,

Below is a very interesting VBA procedure that would remove any text between brackets:

     Sub supprparen()
        For Each cel In Range("A:A")
           Do While InStr(cel, "(") > 0
              cel.Value = Replace(cel, Mid(cel, InStr(cel, "("), InStr(cel, ")") - InStr(cel, "(") + 1), "")
           Loop
        Next cel
     End Sub

For example, the following text:
OMI (ANNEM) (DRAMA EDIT V.)

Will become:
OMI

I need to revise the above procedure in order to adress two small issues:

1) Some text will have one bracket only "(" without the end bracket ")". Following is an example:
OMI (ANNEM) (DRAMA EDIT V


 This causes the procedure to stop with an error. Accordingly, we need to handle the error by either leaving the text as is or removing it "(DRAMA EDIT V"

2) The other issue is that I need to keep the season information with the TV program name. For example:
Desperate Houswives (7)  ---> This means Season 7, and I have to keep it, so I want to build some logic into the procedure that if the content between the brackets is numeric, then keep it.

Appreciate your help.
Hani
0
Comment
Question by:Mehawitchi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37709173
For the first issue

     Sub supprparen()
        For Each cel In Range("A:A")
           Do While InStr(cel, "(") > 0 and InStr(cel, ")") > 0
              cel.Value = Replace(cel, Mid(cel, InStr(cel, "("), InStr(cel, ")") - InStr(cel, "(") + 1), "")
           Loop
        Next cel
     End Sub
0
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 200 total points
ID: 37709184
I do not have access to excel at the moment but try replacing

cel.Value = Replace(cel, Mid(cel, InStr(cel, "("), InStr(cel, ")") - InStr(cel, "(") + 1), "")


with

if val(Mid(cel, InStr(cel, "(")+1, InStr(cel, ")") - InStr(cel, "(") - 1))>0 then
   cel.Value = Replace(cel, Mid(cel, InStr(cel, "("), InStr(cel, ")") - InStr(cel, "(") + 1), "")
end if
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 300 total points
ID: 37709200
Try this:
Sub supprparen()
    Dim RegExp           As Object
    Dim strPattern       As String
    Set RegExp = CreateObject("vbscript.regexp")
    strPattern = "(\([^\d]+\))"
    With RegExp
        .Global = True
        .Pattern = strPattern
        For Each cel In Range("A:A")
            If Len(cel.Value) > 0 Then cel.Value = .Replace(cel.Value, "")
        Next cel
    End With
End Sub

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:Mehawitchi
ID: 37709316
Thanks ssaqibh/rorya - Both your solutions for first problem worked.

As for 2nd problem (keeping the brackets that include number), the solution suggested by ssaqibh got me into an endless loop without solving the issue.

I understand you (ssaqibh) don't have access to Excel for trial now, but once you have a chance, you probably need to tweak it a little bit

Thank you
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 37709335
Mine should work for numbers inside brackets.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37709432
Ok Try this

     Sub supprparen()
        For Each cel In Range("A:A")
           ss = ""
           Do While InStr(cel, "(") > 0 And InStr(cel, ")") > 0
                If Val(Mid(cel, InStr(cel, "(") + 1, InStr(cel, ")") - InStr(cel, "(") - 1)) > 0 Then
                    ss = ss & Mid(cel, InStr(cel, "("), InStr(cel, ")") - InStr(cel, "(") + 1)
                End If
                   cel.Value = Replace(cel, Mid(cel, InStr(cel, "("), InStr(cel, ")") - InStr(cel, "(") + 1), "")
           Loop
           If ss <> "" Then cel.Value = cel.Value & ss
        Next cel
     End Sub
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question