LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Count the duplicate string in the cell

On Sat, 20 Oct 2012 07:11:50 +0000, tamer wrote:


i need to count how many times a word is in string in the same cell

so is their way to do that either by funtions or macros


Hans solution will count the instances of a substring within a string. So given:

A1: Is this a good time for a timely discussion about timepieces.
B1: time

Although the substring "time" occurs three times, I would consider that the word "time" only occurs once.

If that is what you mean, then I would recommend a User Defined Function (UDF)

To enter this User Defined Function (UDF), <alt-F11 opens the Visual Basic Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.

To use this User Defined Function (UDF), enter a formula like

=WordCount(A1,B1)

where A1 is the StringToSearch and B1 contains the WordToCount

in some cell.

==================================
Option Explicit
Function WordCount(StringToSearch As String, WordToCount As String) As Long
Dim re As Object, mc As Object
Dim sPat As String
sPat = "\b" & WordToCount & "\b"
Set re = CreateObject("vbscript.regexp")
With re
.Pattern = sPat
.ignorecase = True
.Global = True
End With

Set mc = re.Execute(StringToSearch)
WordCount = mc.Count

End Function
==========================
 
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
Count based on string in cell jmaj Excel Worksheet Functions 2 December 3rd 09 06:04 AM
count no. of commas (,) in a string in a cell hsg Excel Worksheet Functions 3 March 24th 09 06:51 PM
Count duplicate cell values per month Derek Excel Worksheet Functions 7 November 26th 07 06:53 PM
Count occurences of string in cell GettingThere Excel Programming 2 January 12th 07 05:53 AM
How can delete the duplicate word from a string in cell in EXCEL Laxman A Patil Excel Discussion (Misc queries) 1 May 12th 06 03:04 PM


All times are GMT +1. The time now is 01:30 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"