View Single Post
 
Old 11-16-2014, 04:45 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub Count_Column_Text()
Dim TargetText As String, StrMsg As String, 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
    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")
      For i = 1 To .Cells.Count
        Set Rng = .Cells(i).Range
        With .Cells(i).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
    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 & .Cells(1).ColumnIndex & ".", , "Text search in column"
  End If
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote