Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Character is Uppercase 3

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Is there a routine in VBA that will allow me to check a character to see if it is Uppercase or Lowercase? I thought of checking the ASCII representation but is there an easier method?

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
If you want to explicitly check to see if a char is upper or lower case, I don't know of any other way than using ASCII codes:

Sub tester()
mStr = "This Is A Test"
For i = 1 To Len(mStr)
Select Case Asc(Mid(mStr, i, 1))
Case 65 To 90
MsgBox "Upper"
Case 97 To 122
MsgBox "Lower"
Case Else
MsgBox "Not an alpha character"
End Select
Next i
End Sub

If you just want to compare 2 strings that need to be in the same case, you can just use

If UCase(activecell.text) = "HELLO" then
etc etc

Rgds
Geoff
Si hoc legere scis, nimis eruditionis habes
 
Thanks for confirming my suspicions Geoff - I'm going for the ASCII method. Is there a character data type in VBA - I can't seem to find it?! I've written an IsUppercase function which (ideally) would take as input a character data type and returns a boolean. I have everything in place except the input parameter because I can't seem to find this character data type!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Not sure what you mean as a character data type ??
Function isUpper(mstr as string)as boolean

Select Case Asc(mStr)
Case 65 To 90
isUpper = true
Case 97 To 122
isUpper = false
Case Else
isUpper = false
End Select

End Sub

should do shouldn't it ?? guess it depends how you're passing in the test strings

Rgds
Geoff
Si hoc legere scis, nimis eruditionis habes
 
Eh?

Code:
Dim l_str As String, l_or_u As Boolean
l_str = "lower"
l_or_u = (LCase(l_str) = l_str)
msgbox l_or_u

Gives me a messagebox which says "True". I'm pretty sure it can be tinkered with to give you what you want.

::)

 
oooh - like it Bryan - you always come up with something a bit different

Rgds
Geoff
Si hoc legere scis, nimis eruditionis habes
 
Thanks guys (great lateral thinking Bryan!), here is what I ended up with (I could have used Geoff's ASCII technique but it seemed Bryan's shorter version was more efficient).
Code:
Public Function IsUppercase(AString As String) As Boolean
  IsUppercase = (UCase(AString) = AString)
End Function
The question I was asking earlier Geoff was whether there's a character data type (i.e. a string with length 1). Delphi, C++ and Java all have a char type so I presumed that VB would have one! I only really wanted one character at a time to be inputted into the function but this revised method makes much more sense because I need to check the case of the first 3 characters of a username so now I only need to make a single call (instead of 3 calls) like this and it all works a treat:
Code:
  IsUppercase(Mid$(loginName, 1, 3))

Thanks again guys - a star for you both!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top