Solved

Data Cleansing & Text Formulas with Excel 2010

Posted on 2012-03-12
6
348 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now