Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 02-11-2022, 04:37 AM
gmayor's Avatar
gmayor gmayor is offline Kindly help references list Windows 10 Kindly help references list Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Some of your paragraphs have one name and some more than two. The following will replace the last comma where appropriate with 'and'.
The '!' character is not required here, but see Replace using wildcards

Code:
Sub Macro1()
Dim oPara As Paragraph
Dim oRng As Range, oLink As Range
Dim i As Long, j As Long, k As Long, m As Long
Dim vPara As Variant
Dim sText As String
For i = 1 To ActiveDocument.Paragraphs.Count
        Set oRng = ActiveDocument.Paragraphs(i).Range
        oRng.End = oRng.End - 1
        sText = ""
        vPara = Split(oRng.Text, Chr(44))
        j = UBound(vPara) - 1
        For k = 0 To j - 1
            sText = sText & vPara(k)
            If k < j - 1 Then sText = sText & Chr(44)
        Next k
        If j = 0 Then
            sText = vPara(j) & Chr(44)
        Else
            sText = sText & " and" & vPara(j) & Chr(44)
        End If
        Set oLink = ActiveDocument.Paragraphs(i).Range
        oLink.End = oLink.End - 1
        m = InStrRev(oLink, Chr(44))
        oLink.MoveStart wdCharacter, m
        oLink.Copy
        oRng.Text = sText
        oRng.Collapse 0
        oRng.Paste
    Next i
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert manual cross references in footnotes to other footnotes to automatic cross references ghumdinger Word VBA 7 11-20-2014 11:47 PM
Kindly help references list Kindly repair these .docx file alexcj37 Word 1 02-07-2014 08:25 PM
Kindly help references list Section references in a multilevel list Crumbar Word 2 02-24-2013 01:26 PM
Kindly help references list Kindly help me answer these questions Twene Excel 1 11-20-2011 02:18 AM
Kindly help references list kindly fix my problem its urgent! SyedaAdil Office 1 11-04-2009 08:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:28 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft