Removing Part of Cell/Text

RJB78

Board Regular
Joined
Aug 8, 2016
Messages
69
Office Version
  1. 365
Hello - Trying to come up with formula that will give me the number from a different cell, but the cell I am trying to reference also had letters,

Example: The cell referenced, lets call it A1, contains: '4 yrs'. I would like cell A2 to calculate 4. I tried doing a =LEFT(A1,1), but the issue I ran into was double digit numbers ie 10 yrs., would only return the first digit, 1.

Thank you in advance!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Is this what you are looking for...
Excel Formula:
=LOOKUP(9.9E+307,--LEFT(MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),99),ROW($1:$99)))
This is not very robust though. In the examples below ..
  • The first example fails if a couple of rows are inserted above
  • The second one fails outright
The first issue is easy enough to resolve, but the second is not so simple to solve with worksheet formulas. For one thing, whether decimal numbers might be included or not could change the approach.

21 09 18.xlsm
AB
1a 5 b 35
2xx 1 decade44531
Sample
Cell Formulas
RangeFormula
B1:B2B1=LOOKUP(9.9E+307,--LEFT(MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),99),ROW($1:$101)))
 
Upvote 0
With regex. Catches decimal numbers as well.
Did not manage to put it in one pattern, which is probably possible.

VBA Code:
Function jec(cell As String) As Double
 On Error Resume Next
  With CreateObject("VBscript.RegExp")
    .Global = True
    .Pattern = "\d+\.(\d+)"
     a = .Execute(cell)(0)
    .Pattern = "\d+"
     If IsEmpty(a) Then
        jec = .Execute(cell)(0)
     Else
        jec = a
     End If
  End With
End Function
 
Upvote 0
Did not manage to put it in one pattern, which is probably possible.
It certainly is, also catches negative numbers & handles if no numbers at all

VBA Code:
Function GetNum(s As String) As Variant
  Dim RX As Object
  
  GetNum = ""
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "\-?\d+(\.\d*)?"
  If RX.Test(s) Then GetNum = Val(RX.Execute(s)(0))
End Function

21 09 18.xlsm
AB
1a 5 b 35
2xx 1 decade1
3 
4a 1.23561.2356
5avv -67.1434 abc-67.1434
6xxx 
Sample
Cell Formulas
RangeFormula
B1:B6B1=GetNum(A1)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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