Number of upper case letters in a string

Ceeyee

Board Regular
Joined
Feb 2, 2011
Messages
164
If I need to find the number of upper cased letters in a string, do I have to use a loop or there is quicker ways?

Thanks.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Maybe this UDF funtion

Code:
Function countUpper(t As String) As Long
 
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = False
        .MultiLine = False
        .Pattern = "[A-Z]"
        countUpper = .Execute(t).Count
    End With
 
End Function

Usage

A B
<TABLE style="WIDTH: 158pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=211><COLGROUP><COL style="WIDTH: 110pt; mso-width-source: userset; mso-width-alt: 5376" width=147><COL style="WIDTH: 48pt" width=64><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; WIDTH: 110pt; HEIGHT: 15pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 height=20 width=147>This is Test1, OK? YES!</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; WIDTH: 48pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 width=64 align=right>7</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 height=20>This is Test2, Ok? Yes!</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 align=right>4</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 height=20>This is Test3, ok? yes!</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63 align=right>2</TD></TR></TBODY></TABLE>

B1
=countUpper(A1)
copy down

HTH

M.
 
Upvote 0
Another VBA option...

Code:
Function UCaseCount(strTest As String)
Dim RegExp As Object
With CreateObject("vbscript.regexp")
    .Pattern = "[^A-Z]"
    .Global = True
    UCaseCount = Len(.Replace(strTest, ""))
End With
End Function
 
Upvote 0
@T. Valko

Could you provide a formula? I'm curious.

M.
Here are a couple of examples.

This one needs array entered**:

=COUNT(FIND(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

You can also use a named string:
  • Letters
  • Refers to: ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Then, array entered**:

=COUNT(FIND(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1),Letters))

** array formulas need to be entered using the key
combination of CTRL,SHIFT,ENTER (not just ENTER).
Hold down both the CTRL key and the SHIFT key
then hit ENTER.

And this normally entered version for upper case characters:

=SUMPRODUCT(--(ABS(CODE(MID(A2,ROW(INDEX(A:A,1):INDEX(A:A,LEN(A2))),1))-77.5)<=12.5))

And this normally entered version for lower case characters:

=SUMPRODUCT(--(ABS(CODE(MID(A2,ROW(INDEX(A:A,1):INDEX(A:A,LEN(A2))),1))-109.5)<=12.5))
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

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