Solved

Data Cleansing & Text Formulas with Excel 2010

Posted on 2012-03-12
6
353 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

828 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