Consulting

Results 1 to 8 of 8

Thread: Solved: Replace All count

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Solved: Replace All count

    Hi all,
    When you run Find and Replace, a dialog box advises the number of replacements made. Can this number be returned by the VBA code as a variable?
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I would like to point out, regarding that link, that it is very easy to get a count of a word in a document with MUCH less code, and WITHOUT using Replace, and WITHOUT doing all that processing with the counting of characters. Bleeech, blaaack![vba]Sub CountOfWords()
    Dim iCount As Integer
    Dim strIn As String
    strIn = InputBox("Enter a word to count.")
    With ActiveDocument.Content.Find
    Do While (.Execute(FindText:=strIn, Forward:=True) _
    = True) = True
    iCount = iCount + 1
    Loop
    End With
    MsgBox "There are " & iCount & " instances of the word " & strIn
    End Sub[/vba]

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Just for fun, what about this?

    [vba]Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 01/02/2006 by Egbert
    '
    Dim WasTracking As Boolean
    Dim RevisionCount As Long

    WasTracking = ActiveDocument.TrackRevisions
    RevisionCount = ActiveDocument.Revisions.Count
    ActiveDocument.TrackRevisions = True

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "anthill"
    .Replacement.Text = "foible"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    RevisionCount = (ActiveDocument.Revisions.Count - RevisionCount) / 2
    If Not WasTracking Then
    WordBasic.AcceptAllChangesInDoc
    ActiveDocument.TrackRevisions = False
    End If
    MsgBox "Word has completed its search of the document and has made " & _
    RevisionCount & " changes"

    End Sub[/vba]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Straight after posting I realised this was incomplete. Even if Track Changes is Off there may be Revisions in the Document, so some extra code is needed to deal with that.

    As it was only for fun in the first place, I leave you to do that for yourself.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Nice one Tony.

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Both,
    I was using the search replace anyway so I thought the extra loop an unnecessary step, but time is not much of an issue here. I'll have a play with the Revisions method as well.
    Gerry,
    I was incorporating this in that XML tags question, but I've not had a response.
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Gerry,
    My problem in counting was the inclusion of brackets, since I've now found out to use \<*\>, this makes life simpler.
    Regards
    Malcolm

    (memo to self RTFM )
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •