#1
|
|||
|
|||
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
|
||||
|
||||
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
|
||||
|
||||
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] |
|
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, select, and replace part of text with bold | paik1002 | Word VBA | 4 | 12-07-2015 11:24 PM |
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 in bold | redzan | Word VBA | 1 | 07-27-2014 03:35 PM |
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 |