Solved

Data Cleansing & Text Formulas with Excel 2010

Posted on 2012-03-12
6
349 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 2016 Excel Issue 4 26
Split a string in excel 9 33
Excel copy picture into Outlook email 7 45
Error in formula not increasing value 2 15
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

920 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

12 Experts available now in Live!

Get 1:1 Help Now