PDA

View Full Version : [SOLVED:] Search selection & replace with incrementing numbers



Ross Webb
08-26-2014, 06:30 PM
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.

macropod
08-26-2014, 08:55 PM
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

Ross Webb
08-27-2014, 03:45 PM
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

macropod
08-27-2014, 04:07 PM
Have you had experience with SFM before?
I don't even know what SFM is.

Ross Webb
08-27-2014, 04:27 PM
Ha! I guess you are a good guess then. SFM = Standard Format Markers (see paratext dot org/about/usfm).
But thanks. I learn!
Ross

macropod
08-27-2014, 05:08 PM
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.

Aju0652
01-27-2021, 12:27 PM
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?27816

macropod
01-27-2021, 03:17 PM
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.