count how often a letter appears within text

A

astrologer

ROAR for R is equal to 2 . OAR for R is equal to 1. ORR for R is equal to 2.
The totals are R = 5, O = 3 and A = 2 appearances. What formulae can I use
to count how many times a letter appears in a text?

----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/com...-a6142c1157dc&dg=microsoft.public.word.tables
 
M

macropod

At the most basic level, select the text and use Find/Replace to replace the
character with itself. Word will tell you how many occurrences were
replaced.

Cheers
 
K

Klaus Linke

macropod said:
At the most basic level, select the text and use Find/Replace to replace
the character with itself. Word will tell you how many occurrences were
replaced.


If you want to get more sophisticated, you'd need a macro. The one below
should do the trick, and append a table with the results at the end of the
document.

If you need help with macros, see
http://word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm

Regards,
Klaus


Sub CodesFast()
Dim myString, myStringNew, myChar, myCode
Dim strOutput, HexString, myCharCount
myString = ActiveDocument.Content.Text
strOutput = ""
Do
myChar = left$(myString, 1)
myStringNew = Replace(myString, myChar, "", 1, _
Compare:=vbBinaryCompare)
myCharCount = Len(myString) - Len(myStringNew)
myCode = AscW(myChar) And &HFFFF&
strOutput = strOutput & (myCode) & vbTab
StatusBar = myCode
HexString = Hex$(myCode)
While Len(HexString) < 4
HexString = "0" & HexString
Wend
strOutput = strOutput & "U+" & HexString & vbTab
If myCode > 31 Then
strOutput = strOutput & myChar
End If
strOutput = strOutput & vbTab & LTrim(str$(myCharCount))
strOutput = strOutput & vbCr
myString = myStringNew
Loop Until Len(myString) = 0
ActiveDocument.Content.Select
Selection.Collapse Direction:=wdCollapseEnd
Selection.Range.InsertParagraphBefore
Selection.TypeText Text:=" "
Selection.Expand Unit:=wdParagraph
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Codes"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.Collapse Direction:=wdCollapseStart
Selection.TypeText strOutput
Selection.GoTo What:=wdGoToBookmark, Name:="Codes"
Selection.ConvertToTable Separator:=wdSeparateByTabs
Selection.Sort ExcludeHeader:=False, FieldNumber:=1, _
SortFieldType:=wdSortFieldNumeric, _
SortOrder:=wdSortOrderAscending
Selection.Rows.ConvertToText Separator:=wdSeparateByTabs
ActiveDocument.Bookmarks("Codes").Delete
End Sub
 

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