Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    May 2003
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Finding text in tables (WordXP)

    Is there a way to search for text in a table, where:
    Cell(1) = "abc" AND Cell(3) ="123" AND Cell(9) = "xyz"
    Will I have to write a macro to first find all rows where cell(1) ="abc" then, look through these to find all with cell(3) = "123"? then look through the result to find cell(9) = "xyz"?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Finding text in tables (WordXP)

    I don't think the search feature can handle noncontiguous groups of text. If the goal is to filter down to a small part of a big set of rows, Excel might handle it more efficiently.

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Finding text in tables (WordXP)

    You are going to need a macro to perform a search like this. What is it that you actually want to do to the table that fulfills these requirements?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Finding text in tables (WordXP)

    Here's a macro. I didn't really test it very hard, but you can see how it works in the attached document.
    <pre>Option Explicit
    Type udtCriteria
    col As Integer 'column number
    val As String 'value of cell (as string)
    cs As Boolean 'case sensitive search?
    End Type

    Sub testSearch()
    'Place the insertion point in the first table for testing
    'First collapse to the end if there is a selection; this enables
    'running it again to match the next matching row
    If Selection.Type <> wdSelectionIP Then
    Selection.Collapse wdCollapseEnd
    End If
    'Populate an array of criteria for matching
    Dim arrCriteria(2) As udtCriteria
    arrCriteria(0).col = 1
    arrCriteria(0).val = "abc"
    arrCriteria(0).cs = False
    arrCriteria(1).col = 3
    arrCriteria(1).val = "123"
    arrCriteria(1).cs = False
    arrCriteria(2).col = 9
    arrCriteria(2).val = "xyz"
    arrCriteria(2).cs = False
    'Run the matching function and report the result
    If SelectFirstMatchingRow(arrCriteria()) = True Then
    MsgBox "Found a matching row"
    Else
    MsgBox "Could not find a matching row"
    End If
    End Sub

    Function SelectFirstMatchingRow(arr() As udtCriteria) As Boolean
    'This works only within a table
    If Not Selection.Information(wdWithInTable) Then
    SelectFirstMatchingRow = False
    Exit Function
    End If
    'Set up to loop through the rows and then the criteria
    Dim strArray() As String, intCount As Integer
    Do
    Selection.Rows(1).Select
    strArray = Split(Selection.Text, Chr(13) & Chr(7))
    For intCount = LBound(arr) To UBound(arr)
    'Bounce out if the rows fails on any criterion
    If StrComp(strArray(arr(intCount).col - 1), arr(intCount).val, _
    vbTextCompare + arr(intCount).cs) <> 0 Then Exit For
    'Current criterion passed, but only success if we matched the last one
    If intCount = UBound(arr) Then 'we matched 'em all
    'Leave the row selected
    SelectFirstMatchingRow = True
    Exit Do
    End If
    Next
    'Navigate to next row, if we're beyond the table, exit
    Selection.Collapse wdCollapseEnd
    If Selection.Information(wdWithInTable) = False Then
    SelectFirstMatchingRow = False
    Exit Do
    End If
    Loop
    End Function</pre>

    The idea behind the case sensitivity parameter for StrComp is that in Visual Basic, False = 0 and True = -1, so 1+0 = vbTextCompare, and 1+(-1) = 0 = vbBinaryCompare. It works in theory. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Attached Files Attached Files

  5. #5
    2 Star Lounger
    Join Date
    May 2003
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding text in tables (WordXP)

    Jefferson,

    Many thanks for providing that solution.

    <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •