Extract Number from AlphaNumeric string

Posted on 2014-01-29
Last Modified: 2014-01-29
excel 2010 vba:

What I need:
str2  is a string variable
falpha is just my holder variable

str2 usually is  something like:   B2  C3   D34   B21   AC3    DE45    etc...

What I need:
I need a variable just to hold the Numeric part of the str2

The code below with extract the alpha part i need
If IsNumeric(Mid(str2, 2, 1)) Then
            falpha = Left(str2, 1)
              falpha = Left(str2, 2)
       End If
          str = falpha

Open in new window

Question by:fordraiders
LVL 40

Assisted Solution

als315 earned 50 total points
ID: 39818588
Dim i As Integer
Dim Str As String, c As String
Dim falpha As String, fnumeric As String
falpha = ""
fnumeric = ""
For i = 1 To Len(Str2)
    c = Mid(Str, i, 1)
    If IsNumeric(c) Then
        fnumeric = fnumeric & c
        falpha = falpha & c
    End If
Next i
Debug.Print falpha, fnumeric

Open in new window

LVL 50

Accepted Solution

Rgonzo1971 earned 400 total points
ID: 39818610

pls try

Function FindNumber(MyString As String) As Double
strFormula = "=1*MID(A1,MATCH(TRUE,ISNUMBER(1*MID(A1,ROW($1:$256),1)),0),COUNT(1*MID(A1,ROW($1:$256),1)))"
strFormula = Replace(strFormula, "A1,", Chr(34) & MyString & Chr(34) & ",")
FindNumber = Evaluate(strFormula)
End Function

Open in new window

LVL 35

Assisted Solution

mvidas earned 50 total points
ID: 39818697
Simply because I couldn't pass up this question without giving a Regular Expression answer:
Function ExtractNumber(str2 As String) As String
 Dim RegEx As Object
 Set RegEx = CreateObject("vbscript.regexp")
 RegEx.Global = True
 RegEx.Pattern = "\D"
 ExtractNumber RegEx.Replace(str2, "")
End Function

Open in new window

It will return whatever numbers are in the string, regardless of what is sent:
 Debug.Print ExtractNumber("B2")
 Debug.Print ExtractNumber("C3")
 Debug.Print ExtractNumber("DE45")
 Debug.Print ExtractNumber("B2  C3   D34   B21   AC3    DE45")

Open in new window


Author Closing Comment

ID: 39818707

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
question about excel functionality 3 31
Time difference between dates without weekend 33 72
How do I extract data from Excel worksheets? 5 39
Excel Calculation 4 48
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

808 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