VBA to Pull Only Numbers From a Text String with Number In It.

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Code:
Option Explicit

Function StripChar(Txt As String) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\D"
StripChar = .Replace(Txt, "")
End With
End Function
 
Sub PullNumbers()
    Range("E2").Select
    ActiveCell.Formula = "=StripChar(A2)"
    Range("E2").Select
    Selection.AutoFill Destination:=Range("E2:E8"), Type:=xlFillDefault
    Range("E2:E8").Select
    Range("A1").Select
End Sub

So I'm using the above code to pull numbers from a text string that has numbers mixed in with it.
The problem is if I have a decimal place. The code will still pull the numbers to the right of the decimal and this I do not want.
For example, if I have:

[TABLE="width: 201"]
<tbody>[TR]
[TD="class: xl63, width: 201"]ahrihpxf9c / 1d.123

the code will pull back: 91123

But I only want 91

How can I trunc this number, or use the =LEFT...or something?
Thanks for the help[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Will your text ever have periods (dots that are not decimal points)?
Unfortunately yes...each text may have a decimal point, or it may not. The text strings are random.
Thanks for the help

I just reread your question. The text string is not in the form of a sentence. Each cell contains a mixture of letters and numbers mixed together. Some of the letters/numbers may have a decimal point, or it may not.
At any rate, each data set of letters/numbers will always be confined to one cell.
Thanks
 
Last edited:
Upvote 0
I just reread your question. The text string is not in the form of a sentence. Each cell contains a mixture of letters and numbers mixed together. Some of the letters/numbers may have a decimal point, or it may not.
That does not address what I was actually after. Could you have text like this and, if so, what would you want as an answer for them...

abc.de93fg3.49

a9bc.de87fg.h65ij
 
Upvote 0
That does not address what I was actually after. Could you have text like this and, if so, what would you want as an answer for them...

abc.de93fg3.49

a9bc.de87fg.h65ij

I would not see text like this, with two periods / decimal points. Only one decimal point, and I would be looking for numbers to the left of the decimal point.
Thanks for the help
 
Last edited:
Upvote 0
I would not see text like this, with two periods / decimal points. Only one decimal point, and I would be looking for numbers to the left of the decimal point.
In that case, I think this function will do what you want...
Code:
Function StripChar(Txt As String) As String
  Dim X As Long
  For X = 1 To InStr(Txt & ".", ".") - 1
    If Mid(Txt, X, 1) Like "#" Then StripChar = StripChar & Mid(Txt, X, 1)
  Next
End Function
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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