If i have a string such as "1A2D3B". If i want it to fetch the 1st, 2nd or 3rd number for the left and assign it to N. What code should i type in?
Printable View
If i have a string such as "1A2D3B". If i want it to fetch the 1st, 2nd or 3rd number for the left and assign it to N. What code should i type in?
something like
VB Code:
n = Left(String, 1) n = n & Left(String, 3) n = n & Left(String, 5)
What is the purpose of this?
What is N?
How will the string be formatted?
Will the format always be the same?
Where do you get this string from?
What does it represent?
How should the numbers be 'assigned'?
Should each overwrite wha is in N?
Should they be added to eachother?
Should they lined up like Paralinx does?
You can test if a character is numeric with the function IsNumeric(String), which returns a boolean.
You can also test if the character's ascii code is within a certain range.
The ascii code can be obtained with Asc(Character) and is an integer.
What jeroen79 is trying to say is that if you are more specific with your question, we can help you a whole lot better. After all, we are programmers, not psychics. ;)
Also, specific yet breif thread titles get you even more help. "Newb question plz answer" isn't a good way to go, because there are many who ignor that thread just because it was called that. We had a discussion on this awhile back. It's in my signature located below in a link ;)
And to help you solve this, this is how you do it:
VB Code:
Option Explicit Private Sub Form_Activate() Dim N As String Dim strVal As String strVal = "1A2D3B" N = Mid(strVal, 1, 1) N = N & Mid(strVal, 3, 1) N = N & Mid(strVal, 5, 1) MsgBox N End Sub
Also, for longer (or shorter) Strings then something like this may help:
VB Code:
Option Explicit Private Sub Form_Load() Dim N As Long N = Extract_Numerals("1A2D3B4C5D6789Z") MsgBox N End Sub Private Function Extract_Numerals(ByVal strToConvert As String) As Long Dim intCount As Integer Dim intIdx As Integer intCount = Len(strToConvert) For intIdx = 1 To intCount If IsNumeric(Mid$(strToConvert, intIdx, 1)) Then Extract_Numerals = Extract_Numerals & Mid$(strToConvert, intIdx, 1) Next End Function