Consulting

Results 1 to 8 of 8

Thread: Search selection & replace with incrementing numbers

  1. #1

    Search selection & replace with incrementing numbers

    I would like to put in a series of incrementing numbers in specific places in a document. For example, if I have the marker "\v" scattered throughout a text I would like to add a '1' to the first one like "\v 1 ", and "\v 2 " at the next instance, and so forth.

    An added complication - which seems to be the downfall of my experiments - is that it needs to happen only within a selection of the available text in a document.

    Whith my macro below, on the first replace, the selection is removed, and thus(?) the macro stops. Whatever I've tried I can't get the replace value to increment, and have the macro continue.

    So far I have:

    Sub Increment_Verse_Number()
    Dim iNextVerse As Integer
    Dim sReplaceText As String
    iNextVerse = 1
    
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
    
        With Selection
            sFindText = "\v "
            sReplaceText = "\v" + Str$(iNextVerse) + " "
            iNextVerse = iNextVerse + 1
            Selection.Find.Text = sFindText
            Selection.Find.Replacement.Text = sReplaceText
            Selection.Find.Execute Replace:=wdReplaceOne
        End With
    End Sub

    Thanks.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could use a macro like:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long, j As Long, Rng As Range
    Set Rng = Selection.Range
    On Error GoTo ErrExit
    i = CLng(InputBox("What is the starting #?"))
    On Error GoTo 0
    With Selection.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "\v "
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        If .InRange(Rng) Then
          .InsertAfter j + i
          j = j + 1
          .Collapse wdCollapseEnd
          .Find.Execute
        Else
          Exit Do
        End If
      Loop
    End With
    ErrExit:
    Application.ScreenUpdating = True
    MsgBox j & " verse references updated."
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thanks Paul. Totally helpful. Just tweaked to add a space after the number and it did the trick. This is going to save me a day's work at least!
    Have you had experience with SFM before? I see you recognised the \v's as verse references.
    Ross

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Ross Webb View Post
    Have you had experience with SFM before?
    I don't even know what SFM is.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Ha! I guess you are a good guess then. SFM = Standard Format Markers (see paratext dot org/about/usfm).
    But thanks. I learn!
    Ross

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Ross Webb View Post
    Ha! I guess you are a good guess then.
    Not really guessing - your own code had 'iNextVerse', so it wasn't too hard to figure out what kind of content you were working with.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Newbie
    Joined
    Jan 2021
    Posts
    3
    Location

    Question Code is not working

    Hi Paul the code is not working. i got an error on line Set Rng = Selection.Range as wrong number of arguents or invalid property assignmnt also please help me ehat to write in this line i = CLng(InputBox("What is the starting #?")) . "What is the starting #?" Means?Capture.jpg

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    There is nothing wrong with the code. Quite clearly, it worked for the OP.


    Since # is the standard shorthand for a number, I'd have thought the meaning of "What is the starting #?" to be obvious.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

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