Check to see if entire string is UpperCase?

M

msnyc07

I know I can check if an individual character is uppercase If X Like "[A-Z]"
but is there a way to check if a string of indeterminate length is all
uppercase?
 
D

Dave Peterson

if ucase(myStr) = mystr then
'it looks the same as uppercase

But if your string looks like:

myStr = "7!@#$_-==--"

Then upper/lower case doesn't really make sense.


I know I can check if an individual character is uppercase If X Like "[A-Z]"
but is there a way to check if a string of indeterminate length is all
uppercase?
 
R

Rick Rothstein

The Like operator allows wildcards...

If Not X Like "*[a-z]*" Then
' X does not contain any lower case letters which
' doesn't mean every letter is an upper case letter...
' there could be punctuation marks or blanks
Else
' X does not contain any lower case letters
End If
 
J

Joe User

msnyc07 said:
I know I can check if an individual character is
uppercase If X Like "[A-Z]" but is there a way
to check if a string of indeterminate length is all
uppercase?

Both of the of following should work, assuming s is String and isUCase is
Boolean. I don't know which is more efficient.

isUCase = (s Like "*[A-Z]*" And Not s Like "*[a-z]*")

isUCase = (s = UCase(s) And s <> LCase(s))

Note that these return False for a string without any uppercase character.
 
R

Rick Rothstein

' X does not contain any lower case letters which
' doesn't mean every letter is an upper case letter...
' there could be punctuation marks or blanks

The above comment should have read this way (with the correction shown in
upper case)...

' X does not contain any lower case letters which
' doesn't mean every CHARACTER is an upper case letter...
' there could be punctuation marks or blanks

--
Rick (MVP - Excel)


Rick Rothstein said:
The Like operator allows wildcards...

If Not X Like "*[a-z]*" Then
' X does not contain any lower case letters which
' doesn't mean every letter is an upper case letter...
' there could be punctuation marks or blanks
Else
' X does not contain any lower case letters
End If

--
Rick (MVP - Excel)


msnyc07 said:
I know I can check if an individual character is uppercase If X Like
"[A-Z]"
but is there a way to check if a string of indeterminate length is all
uppercase?
 
D

Dana DeLouis

I know I can check if an individual character is uppercase If X Like "[A-Z]"
but is there a way to check if a string of indeterminate length is all
uppercase?

Just to throw this out in case one uses 'Option Compare'

Option Explicit
Option Compare Text

Sub Demo()
Dim s
s = "abC"

'True, but not correct
Debug.Print UCase(s) = s

'False..Correct
Debug.Print SameQ(s, UCase(s))
End Sub

Function SameQ(s1, s2)
SameQ = StrComp(s1, s2, vbBinaryCompare) = 0
End Function

= = = = = = =
HTH :>)
Dana DeLouis
 

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