Solved

Extract Number from AlphaNumeric string

Posted on 2014-01-29
4
346 Views
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)
       Else
              falpha = Left(str2, 2)
       End If
       
          str = falpha

Open in new window



Thanks
fordraiders
0
Comment
Question by:fordraiders
4 Comments
 
LVL 39

Assisted Solution

by:als315
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
    Else
        falpha = falpha & c
    End If
Next i
Debug.Print falpha, fnumeric

Open in new window

0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 400 total points
ID: 39818610
Hi,

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

Regards
0
 
LVL 35

Assisted Solution

by:mvidas
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")
 '2
 
 Debug.Print ExtractNumber("C3")
 '3
 
 Debug.Print ExtractNumber("DE45")
 '45
 
 Debug.Print ExtractNumber("B2  C3   D34   B21   AC3    DE45")
 '233421345

Open in new window

Matt
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39818707
Thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

758 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now