LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #17   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,651
Default Count letter string, e.g. h/EL/p

On Mon, 3 Sep 2007 22:36:00 -0700, Lee wrote:

I am counting how often specific digraphs (e.g. er, ou, ought, ow) occur in
the most common 1000 words. Sometimes the digraph will be embedded within a
word and sometimes it occurs at the end.


If my understanding of digraphs is correct, then the only variation you will
need is to separate out digraphs from trigraphs and tetragraphs, so a routine
which allows you to specify a character sequence that may not be followed by
other character(s) should be sufficient.

I also presume that if a word should have two identical digraphs (and I don't
know if any exist), that you would only want to count it once.

That being the case, this UDF should suffice:

To use the UDF, enter a formula of the type:


=SUMPRODUCT(ReCount(range,string_to_find,[string_not_to_follow],[search_entire_string]))

The arguments within brackets [...] are optional.

The [search_entire_string] option will, is set to TRUE, will count multiple
matches within a single string. In other words, looking for "ee" in
beekeeping, the function will return two; otherwise it will return one.

To return all the "ou", you would use:

=SUMPRODUCT(recount(A1:A1000,"ou"))

To return only "ou" not followed by "gh", you would use:

=SUMPRODUCT(recount(A1:A1000,"ou","gh"))

and to count all of the "ou" not followed by "gh" in each word as a separate
entry, you would use:

=SUMPRODUCT(recount(A1:A1000,"ou","gh",TRUE))

As written, the UDF is case insensitive. This could be made an optional
argument, if required.

To enter the VBA code, <alt-F11 opens the VB Editor.

Ensure your project is highlighted in the Project Explorer window, then
Insert/Module and enter the code below into the window that opens:

================================================== ==========
Option Explicit

Function ReCount(s, p, Optional NF = "", Optional g As Boolean = False)
's: the string being searched. It can be a string, _
a cell reference, or a range of cells
'p: the pattern to search for
'NF: an optional pattern that should NOT FOLLOW p
'g: if TRUE, will cause multiple matches within a single string

If NF < "" Then
p = p & "(?!" & NF & ")"
End If
Select Case VarType(s)
Case Is = vbString
ReCount = Cnt(s, p, g)
Case Is vbArray
Dim c As Range, i As Long
Dim t
ReDim t(0 To s.Count)
For Each c In s
t(i) = Cnt(c.Text, p, g)
i = i + 1
Next c
ReCount = t
End Select
End Function

Function Cnt(str, sPattern, g)
Dim re As Object
Dim mc As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = sPattern
re.Global = g
re.ignorecase = True

Set mc = re.Execute(str)
Cnt = mc.Count
End Function
================================
--ron
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count the match letter within a string? Eric Excel Discussion (Misc queries) 7 August 22nd 07 03:43 AM
letter number count Alicia Excel Worksheet Functions 11 May 25th 07 12:21 AM
I WANT TO COUNT HOW MANY TIMES A CERTAIN LETTER APPEARS IN A ROW spring022377 Excel Worksheet Functions 13 February 16th 07 08:39 AM
How can I count the number of times a letter repeats in a string? Wiley Excel Worksheet Functions 3 May 11th 06 06:53 PM
count cells in a coloumn with letter x in it sarg Excel Worksheet Functions 2 September 24th 05 09:01 PM


All times are GMT +1. The time now is 10:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"