In VBA, check if first letter is capitalized (urgent)

Laavista

Board Regular
Joined
Aug 27, 2009
Messages
79
In VBA, I need to check if the first letter of a character string in capitalized.
(I do NOT want to change its case, though)

e.g.,
var1 = "task"
Is the t capitalized?

I found the string function "exact()" and the information on it indicated that the string funciton is case-sensitive, so it can be used to test if the ltter cases are identical to the proper-case version of the string. Their example was checking cell A1.
=EXACT(A1,Proper(A1))

I tried:
If exact(var1,PROPER(var1)) ' this did not work

tried:
dim checkfirstchar as string
Checkfirstchar = exact(A1,Proper(A1)) " (function not defined)

Tried
dim checkfirstchar as string
If Checkfirstchar(exact(A1,Proper(A1))) then " (function not defined)
msgbox "if true, then capitalized)
end if

Your help would be so appreciated. I need this info urgently.
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi Laavista,

Here's one way:

Code:
Sub UpperCaseCheck()

    Dim intASCII As Integer
    
    intASCII = Asc(Left(Range("A1").Value, 1))
    
    Select Case intASCII
        Case 65 To 90 'ASCII Code for A to Z
            MsgBox "First alpha character is capitalised"
        Case Else
            MsgBox "First alpha character is not capitalised"
    End Select

End Sub

HTH

Robert
 
Upvote 0
Code:
Sub testCap()
Dim var1 As String
var1 = "task"
If Left(var1, 1) = LCase(Left(var1, 1)) Then
MsgBox "var1 is in lowercase"
End If
End Sub
 
Upvote 0
In VBA maybe something like:

Code:
x = "something"
If UCase(Left(x,1)) = Left(x,1) Then
    Msgbox "Capital Letter"
Else
    Msgbox "Lowercase Letter"
End if

Treated as a function:
Code:
Function IsFirstLetterCapital(byVal Arg as String) As Boolean
    If Left(arg,1) = UCase(Left(arg,1)) Then
        IsFirstLetterCapital = True
    Else
        IsFirstLetterCapital = False
End If

Edit: aw shucks. Only third place tonight ...
 
Upvote 0
In VBA, I need to check if the first letter of a character string in capitalized.
(I do NOT want to change its case, though)

e.g.,
var1 = "task"
Is the t capitalized?

I found the string function "exact()" and the information on it indicated that the string funciton is case-sensitive, so it can be used to test if the ltter cases are identical to the proper-case version of the string. Their example was checking cell A1.
=EXACT(A1,Proper(A1))

I tried:
If exact(var1,PROPER(var1)) ' this did not work

tried:
dim checkfirstchar as string
Checkfirstchar = exact(A1,Proper(A1)) " (function not defined)

Tried
dim checkfirstchar as string
If Checkfirstchar(exact(A1,Proper(A1))) then " (function not defined)
msgbox "if true, then capitalized)
end if

Your help would be so appreciated. I need this info urgently.
Here's some simple code that you can adapt:
Code:
Sub test()
var1 = "task"
If UCase(Left(var1, 1)) = Left(var1, 1) Then
 MsgBox "First letter capitalized"
Else
MsgBox "First letter not capitalized"
End If

End Sub
The key is to compare UCase of the first letter of the string to the first letter of the string.
 
Upvote 0
You all are AMAZING.

Thank you so much for taking the time to respond so quickly!! I will test these options.

Thanks again!!!
 
Upvote 0
Laavista,


Try:


Code:
Option Explicit
Sub CheckFirstChar()
' hiker95, 06/29/2010, http://www.mrexcel.com/forum/showthread.php?t=477948
Dim MyString As String

MyString = "this is a test"
If Asc(Left(MyString, 1)) > 64 And Asc(Left(MyString, 1)) < 91 Then
  MsgBox "The first letter of '" & MyString & "' is capitalized."
Else
  MsgBox "The first letter of '" & MyString & "' is NOT capitalized."
End If

End Sub
 
Upvote 0
Code:
Function IsFirstLetterCapital(r As String) As Boolean
IsFirstLetterCapital = (Left(r, 1) = UCase(Left(r, 1)))
End Function
 
Upvote 0
solved

I want to thank each of you. I actually tried all the suggestions, and they all worked.

THANK YOU for taking the time to respond to this question. I was able to complete my project because of you.

THANKS.
 
Upvote 0

Forum statistics

Threads
1,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top