Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Extract letters from a string contaning numbers

Posted on 2014-07-25
Medium Priority
159 Views
i have a regristraction routine that check for valid input
I thought the following would work, but if the user only entered numbers and no letters
it could be fooled, how can i make sure the proper letters are input ?

``````Private Sub Command5_Click()
Dim Success As Boolean
Dim Value As String
Dim StringEntered As String
StringEntered = "yjk65kg12dky" 'this will be textbox value entered by user
Value = GetNumbers(StringEntered)

If Val(Value) >= 5000 And Val(Value) <= 9900 Then
'now check if the correct letters are typed
Success = True
Else
Success = False
End If
MsgBox Success

End Sub
Function GetNumbers(Value As String) As String
'extract and return numbers from a string
Dim Index As Long
Dim Digit As String
Dim Final As String
Dim Count As Long

Count = 1
GetNumbers = space(Len(Value))
For Index = 1 To Len(Value)
Digit = Mid(Value, Index, 1)
If Digit Like "[0-9]" Then
Mid(GetNumbers, Count, 1) = Digit
Count = Count + 1
End If
Next

GetNumbers = Left(GetNumbers, Count - 1)
End Function
``````
0
Question by:isnoend2001
[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
• 7
• 5

LVL 49

Expert Comment

ID: 40220437
This will result in just the letters in Final. If you just want numbers then remove the Not.

``````      For Index = 1 To Len(Value)
If Not IsNumeric(Mid\$(Value, Index, 1)) Then
Count = Count + 1
Final = Final & Mid\$(Value, Index, 1)
End If
Next
``````
0

LVL 49

Expert Comment

ID: 40220440
I'm sorry but I see I most likely misunderstood your requirement.

What things would make the input invalid?
0

Author Comment

ID: 40220470
Thanks martin liss
If the user only typed numbers and no letters it could be valid or

StringEntered = "yjk65kg12dky" 'this will be textbox value entered by user
the output would also need the letters "yjkkgdky" and the number value  5000 to 9900
0

LVL 49

Accepted Solution

Martin Liss earned 2000 total points
ID: 40220482
``````Dim Index As Long

Dim Digit As String
Dim strLetters As String
Dim strNumbers As String
Dim Count As Long

For Index = 1 To Len(TextBox1.Text)
If IsNumeric(Mid\$(TextBox1.Text, Index, 1)) Then
'Count = Count + 1 What do you want to count?
strNumbers = strNumbers & Mid\$(TextBox1.Text, Index, 1)
Else
strLetters = strLetters & Mid\$(TextBox1.Text, Index, 1)
End If
Next

MsgBox strNumbers
MsgBox strLetters
``````
0

LVL 49

Expert Comment

ID: 40220513
Or perhaps something like this.

``````    Const Valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

For Index = 1 To Len(TextBox1.Text)
If InStr(1, Mid\$(TextBox1.Text, Index, 1), Valid) = 0 Then
MsgBox "Invalid character '" & Mid\$(TextBox1.Text, Index, 1) & "' found"
Exit Sub
End If
Next
``````
0

Author Closing Comment

ID: 40220525
Thanks MartinLiss
Function GetLetters(value As String)
Dim Index As Long
Dim Count As Long
Dim Final As String
For Index = 1 To Len(value)
If Not IsNumeric(Mid\$(value, Index, 1)) Then
Count = Count + 1
Final = Final & Mid\$(value, Index, 1)
End If
Next
GetLetters = Final

End Function
and checked it against "yjkkgdky"
0

LVL 49

Expert Comment

ID: 40220535
0

Author Comment

ID: 40220538
No your last post allows any numbers or letters.
I need to restrict the input to values  to be between 5000 to 9900 and must contain
the letters: "yjkkgdky"
0

LVL 49

Expert Comment

ID: 40220545
Ok then I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Author Comment

ID: 40220552
What does this mean  "yjk#k@gdky"
Hope you can answer my next question i am going to post. its regarding this same routine
0

LVL 49

Expert Comment

ID: 40220556
I assumed that someone was entering a value containing characters like "@", "#", "\$", etc in a textbox and if so your code in post ID: 40220525 would treat those as non-numeric.
0

Author Comment

ID: 40220569
thats ok if  "@", "#", "\$" it would be invalid mean nothing
The input must contain "yjkkgdky" and the value between 5000 and 9900
0

## Featured Post

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library aâ€¦
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 Month4 days, 14 hours left to enroll