Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 07-25-2017, 08:20 AM
OfficeAssociate99 OfficeAssociate99 is offline Find and Replace rows in a table based on bold text. Windows 7 64bit Find and Replace rows in a table based on bold text. Office 2010 64bit
Novice
Find and Replace rows in a table based on bold text.
 
Join Date: May 2017
Posts: 19
OfficeAssociate99 is on a distinguished road
Default Find and Replace rows in a table based on bold text.


Hi, I am completely new to VBA, so I apologize for any errors/formatting issues.
Basically, I want this program to do this:
1. Search and find a bold keyword (1st category heading)
2. Find the next bold word after (the next category heading)
3. Copy all rows in between the two categories
4. Paste rows in another document (under the 1st category heading)

Master Document: The place I want to input all the new data.

Source Document: Where all the new data is drawn from.

Here is what my tables look like:

Source Document:
Row1: "Aerospace, Space & Defence"
Row2: New Data
Row3: New Data
Row4: "Agri Foods"

I want to copy all the data in between the two headings. Then:

Master Document:
Row1: "Aerospace, Space & Defence"
Row2: Old Data
Row3: "Agri Foods"

I want to paste the new data and replace the old data. Note that the number of rows under each heading varies.

Any help would be greatly appreciated! Thanks so much. Here is my code:

Code:
Const myKeyTerms            As String = _
    "Aerospace, Space & Defence"
Dim myTable                 As Table
Dim myRow                   As Row
Dim myRange                 As Range
Dim myRange2                 As Range
Dim myTable2                 As Table
Dim myRow2                   As Row
Documents.Open ("")
    For Each myTable In ActiveDocument.Tables
        For Each myRow In myTable.Rows
            ' If successful myrange is moved to the found text
            Set myRange = myRow.Range
            ' Search parameters are persistent so you only need to change them if the search parameters change
            With myRange.Find
                .Font.Bold = True
                .Text = ""
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .Wrap = wdFindStop
                .Forward = True
                ' Stop when the range is searched
                ' .Execute returns true if the search is found
                Do While .Execute
                    ' myRange is now the found term
                    myRange.Select
                    If InStr(myKeyTerms, myRange.Text) > 0 Then
                        'Actions to do if the row contained a key term in bold
                       myRange.Copy
                       ThisDocument.Activate
                       For Each myTable2 In ActiveDocument.Tables
                        For Each myRow2 In myTable2.Rows
                            ' If successful myrange is moved to the found text
                        Set myRange2 = myRow2.Range
                            ' Search parameters are persistent so you only need to change 'them if the search parameters change
                             With myRange2.Find
                            .Font.Bold = True
                            .Text = ""
                            .Format = True
                            .MatchCase = False
                            .MatchWholeWord = False
                            .MatchWildcards = False
                            .Wrap = wdFindStop
                            .Forward = True
                            ' Stop when the range is searched
                            ' .Execute returns true if the search is found
                             Do While .Execute
                                ' myRange is now the found term
                                myRange2.Select
                                If InStr(myKeyTerms, myRange2.Text) > 0 Then
                                    Selection.PasteAndFormat (wdTableInsertAsRows)
                                    Exit Sub
                                End If
                          '  reset myRange to encompass the rest of the row
                                myRange2.Start = myRange2.End + 1
                                myRange2.End = myRow2.Range.End
                                
                                myRange2.Select
                                Loop
                                End With
                                Next myRow2
                                Next myTable2
                    End If
                    ' now we need to reset myRange to encompass the rest of the row
                    myRange.Start = myRange.End + 1
                    myRange.End = myRow.Range.End
                                       myRange.Select
                Loop
            End With
        Next myRow
    Next myTable
End Sub
  #2  
Old 07-26-2017, 12:04 AM
gmayor's Avatar
gmayor gmayor is offline Find and Replace rows in a table based on bold text. Windows 10 Find and Replace rows in a table based on bold text. Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
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 of
Default

You have already asked this question - https://www.msofficeforums.com/word-...bold-text.html Those of us who might be able to assist will undoubtedly have seen it and like me recognised that what you ask is impossible without access to the documents. Successful programming relies on certainties that can be tested.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
  #3  
Old 07-26-2017, 06:32 AM
macropod's Avatar
macropod macropod is offline Find and Replace rows in a table based on bold text. Windows 7 64bit Find and Replace rows in a table based on bold text. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

OfficeAssociate99: Kindly don't ask the same question(s) in multiple threads.

Thread closed. You may continue the discussion in your other thread.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Closed Thread



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and Replace rows in a table based on bold text. OfficeAssociate99 Word VBA 4 07-26-2017 07:20 AM
Find and Replace rows in a table based on bold text. Find, select, and replace part of text with bold paik1002 Word VBA 4 12-07-2015 11:24 PM
Find and Replace rows in a table based on bold text. VBA Table – Search All Tables - Find & Replace Text in Table Cell With Specific Background Color jc491 Word VBA 8 09-30-2015 06:10 AM
Find and Replace rows in a table based on bold text. find and replace in bold redzan Word VBA 1 07-27-2014 03:35 PM
Find and Replace rows in a table based on bold text. Word VBA Macro to Find and Replace based on the Alt Text of an Image bennymc Word VBA 1 01-27-2014 04:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:29 PM.


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