Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default testing whether a character is a letter or number

I would like to write a procedure that would test a single character in a
string contained in a worksheet cell to see if it's a number, then test
another character in the same string to see if it's a letter. In this case,
the string to test is located in

Worksheets("dataEntry").Range("testCell")

I would like to test the first character in the string in that cell to see
if it's a number.
I would like to test the second character to see if it's a letter.

If either of these conditions returns false, I'd like the whole expression
to be false.

Thanks in advance.

Paul


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default testing whether a character is a letter or number

set rng = Worksheets("dataEntry").Range("testCell")


if isnumeric(left(rng,1)) and _
lcase(mid(rng,2,1)) < ucase(mid(rng(2,1)) then

--
Regards,
Tom Ogilvy

"Paul James" wrote in message
...
I would like to write a procedure that would test a single character in a
string contained in a worksheet cell to see if it's a number, then test
another character in the same string to see if it's a letter. In this

case,
the string to test is located in

Worksheets("dataEntry").Range("testCell")

I would like to test the first character in the string in that cell to see
if it's a number.
I would like to test the second character to see if it's a letter.

If either of these conditions returns false, I'd like the whole expression
to be false.

Thanks in advance.

Paul




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default testing whether a character is a letter or number

Paul,

Try this code

With Worksheets("dataEntry").Range("testCell")
If IsNumeric(Left(.Value, 1)) Then
If IsText(Mid(.Value, 2, 1)) Then
Debug.Print "Yes"
End If
End If
End With


Private Function IsText(val) As Boolean
IsText = (val = "a" And val <= "z") Or _
(val = "A" And val <= "Z")
End Function


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Paul James" wrote in message
...
I would like to write a procedure that would test a single character in a
string contained in a worksheet cell to see if it's a number, then test
another character in the same string to see if it's a letter. In this

case,
the string to test is located in

Worksheets("dataEntry").Range("testCell")

I would like to test the first character in the string in that cell to see
if it's a number.
I would like to test the second character to see if it's a letter.

If either of these conditions returns false, I'd like the whole expression
to be false.

Thanks in advance.

Paul




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default testing whether a character is a letter or number


"Tom Ogilvy" wrote in message
...


if isnumeric(left(rng,1)) and _
lcase(mid(rng,2,1)) < ucase(mid(rng(2,1)) then


Smart!


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default testing whether a character is a letter or number

a$=yourcell

numvalue= asc(left(a$,1))+asc(mid(a$,1))
if numvalue 9+9 then combination is false


---
Message posted from http://www.ExcelForum.com/



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default one additional question

My thanks to Tom and Bob.

One other question:

I'd also like the expression to return true if the cell has no characters of
any type in it. Can I do that by saying

if rng < "" and_
if isnumeric(left(rng,1)) and _
lcase(mid(rng,2,1)) < ucase(mid(rng(2,1)) then

??


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default testing whether a character is a letter or number

Wouldn't this return true if the entry was either 1a or a1, (which isn't
what I need)?



"dadum1 " wrote in message
...
a$=yourcell

numvalue= asc(left(a$,1))+asc(mid(a$,1))
if numvalue 9+9 then combination is false


---
Message posted from http://www.ExcelForum.com/



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default one additional question

if rng = "" or _
( isnumeric(left(rng,1)) and _
lcase(mid(rng,2,1)) < ucase(mid(rng(2,1))) then

--
Regards,
Tom Ogilvy


Paul James wrote in message
...
My thanks to Tom and Bob.

One other question:

I'd also like the expression to return true if the cell has no characters

of
any type in it. Can I do that by saying

if rng < "" and_
if isnumeric(left(rng,1)) and _
lcase(mid(rng,2,1)) < ucase(mid(rng(2,1)) then

??




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default one additional question

if rng = ""

Of course - Duh.

Thanks, Tom.


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default one additional question

Tom - the code you wrote works great:

if rng = "" or _
( isnumeric(left(rng,1)) and _
lcase(mid(rng,2,1)) < ucase(mid(rng(2,1))) then


I did find a typo in the last expression - (the one to the right of the
inequality):

ucase(mid(rng(2,1)) should be written as ucase(mid(rng,2,1))

Thanks again for your help.

Paul


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the first character a number or letter bactfarmer Excel Worksheet Functions 13 April 22nd 23 09:02 PM
testing for character strings within a cell MKB Excel Worksheet Functions 2 August 29th 06 06:54 PM
Odd! Excel changes my letter character. Why? spygrrl Excel Discussion (Misc queries) 5 March 9th 06 03:53 PM
change headers from letter to number/number to letter lazybee Excel Worksheet Functions 1 July 29th 05 11:08 PM
column header changed from letter to number, how return to letter Ron Excel Discussion (Misc queries) 2 May 9th 05 08:34 PM


All times are GMT +1. The time now is 02:41 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"