Count the alphabetical letters in text

cameron_kyle

Active Member
Joined
Jan 14, 2006
Messages
313
i hope someone may help, this is a very spacific question which may prove to be quite difficult, I am trying to create a VB Script which will count the alphabetical letters in a paragraph of text,


Can anyone please advise me on the best metod of how this could be done, or refer me to a webpage which could help,


kyle
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Code:
option base 1 

Sub testy() 
Dim myparagraph As String, letter_count As Long, a As Long, letter as string 
dim alpha_array(26) as long, alpha_ref as string 
alpha_ref = Array(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) 
myparagraph = [a1] 
letter_count = 0 
a = 1 
Do While a < (Len(myparagraph) + 1) 
letter = Mid(myparagraph, a, 1) 
If letter Like "[A-Za-z]" Then 
    letter_count = letter_count + 1 
    alpha_array(wheretoplace(letter)) = alpha_array(wheretoplace(letter)) + 1 
End If 
a = a + 1 
Loop 

a=1 
do while a < (ubound(alpha_array) + 1) 

msgbox("There are " & alpha_array(a) & " instances of " & alpha_ref(a) & " in this paragraph") 

a=a+1 

loop 

End Sub 

function wheretoplace(letter as string) 
dim n as integer 
dim alpha_refs(26) as string 
alpha_refs = Array(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) 
n=1 
do until alpha_refs(n) = ucase(letter) 
n=n+1 
loop 
wheretoplace = n 
end function

That should work, but I would be tempted to use Hot Peppers more elegant approach...
 
Upvote 0
sorry it doesnt work patrick,

however hotpepper your seems to work great, however it there seems to be a litit to which the macro will count,
 
Upvote 0
my paragraph definatly contains over a two thousand characters, but your macro does not seem to count higher than 840 characters,
 
Upvote 0
for my own sanity if nothing else - this defo works:

Code:
Option Base 1

Sub testy()
Dim myparagraph As String, letter_count As Long, a As Long, letter As String
Dim alpha_array(26) As Long, alpha_ref As Variant
alpha_ref = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O" _
, "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
myparagraph = [a1]
letter_count = 0
a = 1
Do While a < (Len(myparagraph) + 1)
letter = Mid(myparagraph, a, 1)
If letter Like "[A-Za-z]" Then
    letter_count = letter_count + 1
    alpha_array(wheretoplace(letter)) = alpha_array(wheretoplace(letter)) + 1
End If
a = a + 1
Loop

a = 1
Do While a < (UBound(alpha_array) + 1)

MsgBox ("There are " & alpha_array(a) & " instances of " & alpha_ref(a) & " in this paragraph")

a = a + 1

Loop

End Sub

Function wheretoplace(letter As String)
Dim n As Integer
Dim alpha_refs As Variant
alpha_refs = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O" _
, "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
n = 1
Do Until alpha_refs(n) = UCase(letter)
n = n + 1
Loop
wheretoplace = n
End Function
 
Upvote 0
oh yess this works great patrick you are a star, i like the fact that they come up as message boxes,

thank you both so very much
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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