Does this string end with a number?

L

Laura

Greetings,

I'd like to look at a string variable and find out if it ends with a number
(1 through 9). I know how to use the instr function to look for a sub
string, but is there a way to use it (or anything else) to find out if the
last character in the string is ANY number?

I know with the find/replace feature you can turn on "use wildcards" and
then put the acceptable characters in brackets [123456789} and it will look
for any one of them. I'd like to do this with the INSTR function.

THANKS!
 
M

Malcolm Smith

Laura

Off the top of my head (i.e. this has been nowhere near to a compiler):


Function IsLastDigitNumeric (sString as String) as Boolean

dim bRetVal as boolean
dim sChar as string * 1


bretVal = False

if len(sString) > 0 then
sChar = Right$(sString, 1)
if sChar >= 0 and sChar <= 9 then
bRetVal = True
end if
end if

IsLastDigitNumeric = bRetVal
end Function


Hope that this helps. I have replicated your request that the last digit
should be from 1-9, i.e. no zeros. If this is wrong then all you have to
do is to change the if statement.

Regards
- Malc
www.dragondrop.com
 
S

Steve Lang

Hi Laura,

Use Right() to get the last part of the string, then test the ASCII value of
the result using ASC() function. Numbers have ASCII values from 48 (for 0)
to 57 (for 9):

If Asc(Right(strTest, 1))> 47 and asc(Right(strTest, 1))<58 Then
msgbox "strTest ends with a number."
end if

HTH and have a great day!

Steve
 
J

Jay Freedman

Hi Laura,

InStr won't work here because it requires a specific substring. Instead, use
the Like operator, which matches patterns. For your purpose, the # character
matches any digit:

If Right$(MyString, 1) Like "#" Then
MsgBox "The last character is a digit"
Else
MsgBox "The last character is not a digit"
End If

Look up the Like operator in the VBA help for a list of the special pattern
characters such as "#".
 
L

Laura

Thanks so much to all of you -- this is very helpful!

Laura


Jay Freedman said:
Hi Laura,

InStr won't work here because it requires a specific substring. Instead, use
the Like operator, which matches patterns. For your purpose, the # character
matches any digit:

If Right$(MyString, 1) Like "#" Then
MsgBox "The last character is a digit"
Else
MsgBox "The last character is not a digit"
End If

Look up the Like operator in the VBA help for a list of the special pattern
characters such as "#".

--
Regards,
Jay Freedman
Microsoft Word MVP
Greetings,

I'd like to look at a string variable and find out if it ends with a
number (1 through 9). I know how to use the instr function to look
for a sub string, but is there a way to use it (or anything else) to
find out if the last character in the string is ANY number?

I know with the find/replace feature you can turn on "use wildcards"
and then put the acceptable characters in brackets [123456789} and it
will look for any one of them. I'd like to do this with the INSTR
function.

THANKS!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top