Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 07-21-2017, 07:34 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)

Here is the code I have so far:

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

Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and Replace rows in a table based on bold text. Macro to find and replace headings in bold and underline redzan Word VBA 4 02-13-2016 12:24 PM
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 08:12 AM.


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