extract number from text

Shweta

Well-known Member
Joined
Jun 5, 2011
Messages
514
Hi All,

I need a VBA code for extracting number from the given text.

My data is
<TABLE style="WIDTH: 74pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=98 border=0><COLGROUP><COL style="WIDTH: 74pt; mso-width-source: userset; mso-width-alt: 3584" width=98><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; WIDTH: 74pt; BORDER-BOTTOM: #e0dfe3; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" width=98 height=20>Col A
shweta(31056)
</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" height=20>anju(31004)</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" height=20>ankit(31038)</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" height=20>neeru(31029)</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" height=20>roshan(31040)</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" height=20>rupesh31063)</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" height=20>asha(31085)</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" height=20>lokesh(32861)</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" height=20>kavita(31095)</TD></TR></TBODY></TABLE>

I am able to do it with below function.

Function extract_id(s As Range)
Dim i, j As Integer
For j = 1 To Len(s)
If VBA.IsNumeric(Mid(s, j, 1)) Then
extract_id = extract_id & VBA.Mid(s, j, 1)
End If
Next j
End Function

But I want to do it with loop on command button, my coding is

Private Sub CommandButton1_Click()
Dim i, j As Integer
Dim mylr1 As Long
Dim wkst1 As Worksheet
Dim rnge As Range
Set wkst1 = Sheets("c")
mylr1 = wkst1.Range("A" & Rows.Count).End(xlUp).Row
rnge = wkst1.UsedRange.Count
For i = 1 To mylr1 - 1
For j = 1 To Len(rnge)
If VBA.IsNumeric(Mid(rnge, j, 1)) Then
wkst1.Range("C" & i).Value = wkst1.Range("C" & i).Value & VBA.Mid(rnge, j, 1)
End If
Next j
Next i
End Sub

But its not working.

Please help me out.

Thanks,
shweta
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I believe this code will be faster...

Code:
Private Sub CommandButton1_Click()
  With Worksheets("Sheet4").Columns("A")
    .Replace "*(", "", xlPart
    .Replace ")", "", xlPart
  End With
End Sub
 
Upvote 0
Okay, try this code then...

Code:
Private Sub CommandButton1_Click()
  Dim LastRow As Long
  With Worksheets("C")
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
    .Range("B1:B" & LastRow).Value = .Range("A1:A" & LastRow).Value
    With .Columns("B")
      .Replace "*(", "", xlPart
      .Replace ")", "", xlPart
    End With
  End With
End Sub
 
Upvote 0
Thanks Rick,Your coding is working perfectly.

But I want to know can't we do it with just a simple loop. I think this can be done by loop also.

Or I can also create a function in the module and call that function within the loop.

Need your suggestion.


Shweta
 
Upvote 0
I would also like to know if you dont mind why we used xlpart.
I have never used this earlier.
 
Upvote 0
Another VBA option...

Code:
Sub NumsToNextCol()
Dim RE As Object
Dim cCell As Range
Set RE = CreateObject("vbscript.regexp")
Set cCell = [C1]
RE.Pattern = "\D"
RE.Global = True
Do Until cCell.Value = ""
    cCell.Offset(ColumnOffset:=1).Value = RE.Replace(cCell.Value, "")
    Set cCell = cCell.Offset(RowOffset:=1)
Loop
End Sub

Is that something you can work with?
 
Upvote 0
But I want to know can't we do it with just a simple loop. I think this can be done by loop also.
Yes (assuming you consider this loop simpler)...

Code:
Private Sub CommandButton1_Click()
  Dim X As Long, LastRow As Long, OpenParen As Long
  With Worksheets("Sheet4")
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
    For X = 1 To LastRow
      OpenParen = InStr(Cells(X, "A").Value, "(")
      If OpenParen Then Cells(X, "B").Value = Replace(Mid(Cells(X, "A").Value, OpenParen + 1), ")", "")
    Next
  End With
End Sub
The code I posted earlier will be faster (it does its work all at once rather than visiting each cell individually). I'm guessing that you probably will not have enough rows of data to make the time difference noticeable.

I would also like to know if you dont mind why we used xlpart.
I have never used this earlier.

It tells the Replace method to look inside the text in the cell instead of trying to match the entire contents of the cell.
 
Upvote 0
This one is also working fine,

But I am in the initial stage of VBA.So I would like to know if I creat a function in module
Function extract_id(s As Range)
Dim i, j As Integer
For j = 1 To Len(s)
If VBA.IsNumeric(Mid(s, j, 1)) Then
extract_id = extract_id & VBA.Mid(s, j, 1)
End If
Next j
End Function

And call it in my loop

Private Sub CommandButton1_Click()
Dim X As Long, LastRow As Long, OpenParen As Long
With Worksheets("Sheet4")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
For X = 1 To LastRow
Call Extract_id
next X
end with
end sub

Is it possible?
 
Upvote 0
Here is what you're trying to do...

Code:
Function extract_id(s As Range) As String
    Dim returnval As String
    Dim i, j As Integer
    For j = 1 To Len(s.Value)
        If VBA.IsNumeric(Mid(s.Value, j, 1)) Then
            returnval = returnval & VBA.Mid(s, j, 1)
        End If
    Next j
    extract_id = returnval
End Function



Private Sub test()
Dim X As Long, LastRow As Long, OpenParen As Long
With Worksheets("Sheet4")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
For X = 1 To LastRow
    .Range("C" & X).Value = extract_id(.Range("A" & X))
Next X
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,700
Members
452,938
Latest member
babeneker

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