?
Solved

Can you help in extracting the numbers from this string

Posted on 2003-03-14
8
Medium Priority
?
171 Views
Last Modified: 2010-05-01
I have a string

mystring="SEA123_POT

The first 3 letters of my string are always letters
and the remaining between the last letter and the "_" are numbers
I thought of using instr but I dont know how?

I only want to get the numbers in the case above "123"

So i thought of using
Dim TMP As String
TMP = Text1.Text
For a = 65 To 90
TMP = Replace(TMP, Chr(a), "")
Next a

MsgBox TMP

but it gets also the "_" which i dont want.

can you help?
0
Comment
Question by:Gabriel_Brienza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 6

Expert Comment

by:marconovaro
ID: 8135376

Have you got always three letters before the number?

    Dim mystring  As String
    Dim myValue As Long
    Dim pos As Long
   
    mystring = "SEA123_POT"
    pos = InStr(1, mystring, "_", vbTextCompare)
   
    myValue = Val(Mid$(mystring, 4, Len(mystring) - pos))
   
M
0
 

Author Comment

by:Gabriel_Brienza
ID: 8135420
Tried that but doesnt work.

mystring always have
                    1) 3 letters at the beginning
                    2) numbers (which can be many)
                    3) _ (underscore)
                    4) and letters

mystring="sea12112_pot"
or
mystring "sea123_pot"

I only want to get the numbers
 
any more suggestions
thanks
0
 
LVL 6

Accepted Solution

by:
marconovaro earned 80 total points
ID: 8135454

Sorry: this should work:

    Dim mystring  As String
    Dim myValue As Long
    Dim pos As Long
   
    mystring = "SEA1_POT"
    pos = InStr(1, mystring, "_", vbTextCompare)
   
    myValue = Val(Mid$(mystring, 4, pos - 3))

M
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:DocM
ID: 8135456
Hi Gabriel,

Dim TMP As String
TMP = Text1.Text

TMP = Mid(TMP, 4, 3)
 
MsgBox TMP
0
 

Author Comment

by:Gabriel_Brienza
ID: 8135517
thanks a lot it worked

i always have problem with instr

thanks again
gabriel
0
 

Expert Comment

by:livzi
ID: 8135518




mid("ABC123_dfghgsd",4,instr(4,"ABC123_dfghgsd","_") - 4)
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8135530
if you have a string that begins always on "sea" and finishes on "_pot" use following:

mystring = left( mystring, len(mystring) - 4)
mystring = right(mystring, len(mystring) - 3)

or use replace() function
0
 

Author Comment

by:Gabriel_Brienza
ID: 8135568
the letters at the end and at the beginning
can be anything.

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 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