![]() |
|
#5
|
||||
|
||||
|
The code I posted assumes the cells to be tested have been selected. It can therefore work with whatever block of cells you select, be that a single cell, part or all of one or more rows & columns, etc.
To work with all of whatever column the insertion point is in, try: Code:
Sub Count_Column_Text()
Dim TargetText As String, StrMsg As String
Dim Col As Long, i As Long, j As Long, Rng As Range
With Selection
If .Information(wdWithInTable) = False Then
MsgBox "The cursor must be within a table cell.", , "Cursor outside table"
Exit Sub
Else
Col = .Cells(1).ColumnIndex
TargetText = InputBox("Enter text to search. " & _
"This macro will search the column you have " & _
"selected for this text and count how many " & _
"times it appears in that column. Do you want to continue?", _
"Enter text to find and count")
With .Tables(1)
For i = 1 To .Rows.Count
Set Rng = .Cell(i, Col).Range
With .Cell(i, Col).Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = TargetText
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
If .Duplicate.InRange(Rng) Then
j = j + 1
.Collapse wdCollapseEnd
.Find.Execute
Else
Exit Do
End If
Loop
End With
Next
End With
Select Case j
Case 0: StrMsg = "The text " & TargetText & " was not found in the selected column "
Case 1: StrMsg = "There is 1 occurrence of the text " & TargetText & " in the selected column "
Case Else: StrMsg = "There are " & j & " occurrences of the text '" & TargetText & "' in the selected column "
End Select
MsgBox StrMsg & Col & ".", , "Text search in column"
End If
End With
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
| Tags |
| column, count, search |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Macro help - search for value, paste a value in another column
|
IRollman | Excel Programming | 1 | 01-14-2014 01:05 PM |
| Excel Fomula to search for a string and display value from different column | zeeshanbutt | Excel | 1 | 07-29-2012 12:48 AM |
Word 2007: Automatically left-justified centered table or column?
|
wornways | Word | 1 | 03-13-2012 12:18 AM |
Need to search a column for a macth and return a result from a third column
|
pdfaust | Excel | 2 | 02-03-2011 03:02 PM |
| How do I reference a merged cell in a multi column & row table in MS Word ('03')? | jihanemo | Word Tables | 0 | 03-18-2009 08:33 AM |