Regular expression and Macro

ankianand88

New Member
Joined
Sep 27, 2011
Messages
4
I have an excel sheet which contains data in A column.and data is of the form
<TABLE style="WIDTH: 307pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=409 border=0><COLGROUP><COL style="WIDTH: 307pt; mso-width-source: userset; mso-width-alt: 14957" width=409><TBODY><TR style="HEIGHT: 15pt; mso-height-source: userset" height=20><TD class=xl64 style="BORDER-RIGHT: #ece9d8; BORDER-TOP: gray 0.5pt solid; BORDER-LEFT: gray 0.5pt solid; WIDTH: 307pt; BORDER-BOTTOM: gray 0.5pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" width=409 height=20>Sub Total (SEPT MINOR 2011)</TD></TR><TR style="HEIGHT: 15pt; mso-height-source: userset" height=20><TD class=xl64 style="BORDER-RIGHT: #ece9d8; BORDER-TOP: gray; BORDER-LEFT: gray 0.5pt solid; WIDTH: 307pt; BORDER-BOTTOM: gray 0.5pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" width=409 height=20>Sub Total (336103 NOV 11)</TD></TR></TBODY></TABLE>

i need a macro which gives the data in the following format
SEPT MINOR 2011
336103 NOV 11

some guys told me regex can be used..but i have no clue..how to go about it...please help
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Why not simply do a Find / Replace for both unwanted items
 
Upvote 0
Actually there are many entries in the sheet...that was just an example....
The sheet consist of many entries like the two mentioned above
 
Upvote 0
My first answer still applies regardless of how many entries there are.
A simple recorded macro should do it
Code:
Sub Macro1()
    Columns("A:A").Replace What:="Sub Total", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows
    Columns("A:A").Replace What:="(", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows
    Columns("A:A").Replace What:=")", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows
End Sub
 
Upvote 0
Hey thanks its working..but a little glitch...
like some rows contain numeric value like
Sub Total(361628)
and after running the macro am getting
-361628
A minus (-) sign is coming infront of numeric values only
 
Upvote 0
I can't see why this would happen
Have you tried recording a macro yourself. As you can see the replacement does not have any negative signs anywhere in it, it simply replaces with "", which is a blank.
 
Upvote 0
Another variant macro

Code:
Sub test2()
Dim sString
Dim i As Long, a As Long, b As Long, c As Long
 
'Capture inputs
sString = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value
 
For i = 1 To UBound(sString)
a = InStr(Trim(sString(i, 1)), "(") + 1
b = InStr(Trim(sString(i, 1)), ")") - 1
c = b - a
Cells(i, 1) = Mid$(Trim(sString(i, 1)), a, c + 1)
Next i
 
End Sub


Hope dis helps.

Biz
 
Upvote 0
Code:
With Range("A:A")
    .TextToColumns Destination:=.Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="(", FieldInfo:=Array(Array(1, 9), Array(2, 1))
    On Error Resume Next
    .TextToColumns Destination:=.Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :=")", FieldInfo:=Array(Array(1, 1), Array(2, 1))
    On Error GoTo 0
End With
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top