![]() |
|
#1
|
|||
|
|||
![]()
Yes, you've captured the essence of the problem (more or less). I want to identify instances of undefined categories, period. If it occurs like this:
1 Defined Category 2 Data Row 3 Data Row 4 Defined Category 5 Data Row 6 Data Row 7 Undefined Category 8 Data Row Then I just want to copy everything in rows 7 - 8 (or the end of the document). If it occurs like you presented it: 1 Defined category 2 Data row 3 Data row 4 Data row 5 Undefined category 6 Data row 7 Data row 8 Data row 9 Defined category 10 Data row 11 Data row 12 Data row Then I want to select rows 5-8 and copy them. Hope that helps, and thank you for taking the time to help me out ![]() |
#2
|
|||
|
|||
![]()
The code below is closer to your intent.
Code:
Sub sbSearchRowsForBold() ' Consolidate into a single string so we can search using instring to checkif the found text is a Key Term ' If Selection.Find.Text = "Organization" Or Selection.Find.Text = "Date" Or Selection.Find.Text = "Description" Or Selection.Find.Text = "Aerospace, Space & Defence" Or Selection.Find.Text = "Automotive" Or Selection.Find.Text = "Manufacturing" Or Selection.Find.Text = "Life Sciences" Or Selection.Find.Text = "Information Communication Technologies / Digital" Or "Natural Resources / Energy" Or Selection.Find.Text = "Regional Stakeholders" Or Selection.Find.Text = "Other Policy Priorities" Then ' Some of the text entries include ',' so + is used as a seperator Const myKeyTerms As String = _ "OrganizationDate+Description+Aerospace, Space & Defence+Automotive+Manufacturing+Life Sciences+Information Communication Technologies / Digital+Natural Resources / Energy+Regional Stakeholders+Other Policy Priorities" Dim myTable As Table Dim myFirstRange As Range Dim mySecondRange As Range Dim myRemoveRange As Range For Each myTable In ActiveDocument.Tables Set myFirstRange = Nothing Do If myFirstRange Is Nothing Then Set myFirstRange = fnFindBold(mySearchRange:=myTable.Range.Rows(1).Range) Else Set myFirstRange = fnFindBold(mySearchRange:=myFirstRange.Next(unit:=wdRow)) End If ' two possible cases for myFirstrange ' 1. a found range ' 2. nothing - which means we have searched the whole table. If Not myFirstRange Is Nothing Then If InStr(myKeyTerms, myFirstRange.Text) = 0 Then ' Found bold text that is not a defined category (key term) Set mySecondRange = myFirstRange.Duplicate Do Set mySecondRange = fnFindBold(mySecondRange.Next(unit:=wdRow)) Loop Until InStr(myKeyTerms, mySecondRange) > 0 'We have now found text that is a defined category key term Set myRemoveRange = myFirstRange.Duplicate myRemoveRange.End = mySecondRange.Previous(unit:=wdRow).End Set myFirstRange = mySecondRange myRemoveRange.Select myRemoveRange.Cut ' the destiny of the cut text is left to the requirements of the user End If End If Loop Until myFirstRange Is Nothing Next myTable End Sub Function fnFindBold(ByVal mySearchRange As Range) As Range If mySearchRange Is Nothing Then Set fnFindBold = Nothing Exit Function End If With mySearchRange.Find .Font.Bold = True .Text = "" .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .Wrap = wdFindStop .Forward = True .Execute If .Found Then Set fnFindBold = mySearchRange Else Set fnFindBold = fnFindBold(mySearchRange.Next(unit:=wdRow)) End If End With End Function 1. Ranges are pointers to objects so 'set range2=range1' does not create a new range object, it just creates a second pointer to the same object as pointed to by range1. if you want to create a second object for range2 then you must use the duplicate property 'set range2=range2.duplicate'. You might think that you could also say 'set range2 = new range1' but this is not the case. 2. Searching for bold text is a repeated task so it is delegated to a function. 3. fnFindBold is written as a recursive function. You could rewrite this using a do loop. 4. If we ask for the next row in a table and there isn't a next row then 'nothing' is returned and not the range corresponding to the first row of the next table. 5. The code above searches by row, but this possibly isn't necessary. I tested the code above on the table OrganizationDate 2 Data row 3 Data row 4 Data row User defined category 6 Data row 7 Data row 8 Data row Life Sciences 10 Data row 11 Data row 12 Data row and it correctly identifies and cuts the text from 'User defined category' to '8 Data row' from the table. I've left it to your imagination as to what you do with the rows that have been cut from the table. |
![]() |
Tags |
search rows |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
jc491 | Word VBA | 8 | 09-30-2015 06:10 AM |
![]() |
footer-assistance | Word | 1 | 06-29-2015 03:49 AM |
VBA Search Table for Text/Select Text/Insert Hyperlink | sldrellich | Word VBA | 3 | 03-24-2015 01:09 PM |
how to search and replace BOLD text >> font color change? | dylansmith | Word | 4 | 03-12-2013 09:51 PM |
![]() |
stella@happisburgh.net | Excel | 3 | 12-05-2010 08:03 AM |