View Single Post
 
Old 10-18-2023, 10:36 AM
vivka vivka is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Lydia90, the following macro replaces all "Figure (your inputboxed txt)" with "Figure (sequential number)" and makes them captions in the selected range.
Code:
Sub Repl_Stri_W_Sequential_Nums()
'In slection, replace all instances of the inputboxed string
'with sequential numbers / add sequential numbers after each
'instance of the inputboxed string.

Dim myRng As range
Dim stri As String
Dim i As String

Application.ScreenUpdating = False

Set myRng = selection.range
    stri = InputBox("Enter the string to find")
    i = InputBox("Enter the number to start counting from")
    With myRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .text = "Figure " & stri
        .Forward = True
        .Wrap = wdFindStop
        Do
            If .Execute And myRng.InRange(selection.range) Then
                i = i + 1
                myRng = "Figure " & i
                myRng.Style = wdStyleCaption
            Else: Exit Do
            End If
            myRng.Collapse wdCollapseEnd
        Loop
    End With
Application.ScreenUpdating = True
Set myRng = Nothing
End Sub
Reply With Quote