Find text in a column of table

D

Dave Neve

Hi

I know this is a pathetic attempt but it's one thing manipulating code and
quite another to write it.

I want to find words that I have selcected with my cursor in a column of a
table in the same column.


Sub FindAndCompare()
If Selection.Information(wdWithInTable) = True Then
Selection.SelectColumn
End If
Selection.Find.Execute FindText:="", Forward:=True

End Sub

Any help appreciated

Thanks
 
D

Dave Lett

Hi Dave,

If you select the column before running the search, then you will only ever
find the first instance of the text you have selected. If you start the
search from the current selection then the routine will find the next
occurrence of your selected text (at least in my test). I'm not sure how
'refined' you want this to be so I have _not_ accounted for the situation
where the selected text is the last occurrence of that text in the
table/column.

Dim sFindText As String
With Selection
sFindText = .Text
.Find.Execute FindText:=sFindText
End With

HTH,
Dave
 
H

Helmut Weber

Hi Dave,
and there comes another one:
Dim sAny As String
Dim iClm As Integer
Dim iRow As Integer
Dim sClt As String
sAny = Selection.Text
iClm = Selection.Information(wdStartOfRangeColumnNumber)
For iRow = 1 To ActiveDocument.Tables(1).Rows.Count
sClt = ActiveDocument.Tables(1).Cell(iRow, iClm).Range
If InStr(sClt, sAny) Then
ActiveDocument.Tables(1).Cell(iRow, iClm).Select
MsgBox "found"
End If
Next
The funny thing is, searching will always be successful,
because the cell, that containes the text to search for, is
searched too. (Excluding it could easily be done.)
As in former times, searching the memory with debug.
was always successful, as the string to search for, was,
of course, in the memory.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
D

Dave Neve

Hi

Thanks for the code. There is just one question.

When I search for "yoo", it also finds "yoong".

I think I know how to deal with this using

Selection.Find
..Text = "yoo"

but how do I deal with it with the cursor and your code below.

I feel really bad about asking all these questions but I have glanced
through the help pages which are not user friendly and without a garantee of
success. It's easier at the moment for me to just ask.

Thanks
 
H

Helmut Weber

Hi Dave,
here comes another one:
sClt = ActiveDocument.Tables(1).Cell(iRow, iClm).Range
sClt = Left(sClt, Len(sClt) - 2) ' cut off end of cell marker
If sClt = sAny Then
...
But if the cells contain lots of words,
a different and more complicated approach may be appropriate.
There is nothing wrong, by the way, with Dave Lett's suggestion.
My approach just keeps in mind, that there is no column.range,
which may lead to complications in some cases.
I feel really bad about asking all these questions
Don't.

Useful answers make two people feel better. :)

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
D

Dave Neve

Hi

As your code is too complicated for me, I have to check that I have inserted
the second part you sent me in the right place.

I now have


Sub FindInColumn()

Dim sAny As String
Dim iClm As Integer
Dim iRow As Integer
Dim sClt As String
sAny = Selection.Text
iClm = Selection.Information(wdStartOfRangeColumnNumber)
For iRow = 1 To ActiveDocument.Tables(1).Rows.Count
sClt = ActiveDocument.Tables(1).Cell(iRow, iClm).Range
sClt = Left(sClt, Len(sClt) - 2) ' cut off end of cell marker
If sClt = sAny Then
ActiveDocument.Tables(1).Cell(iRow, iClm).Select
MsgBox "found"
End If
Next

End Sub

But it doesn't work (most of the time) There must be a reason concerning
other words in the cell etc as you yourself said might be the case.

So, please let me give you an extract of the table to give you an idea

Interested in
sth + spirit
son* jai*

Interesting
face + sth + spirit
na son* jai*

Invite

chuan

Invited (You're invited to .)
Put infront eg Invited (please) to sit down
cheuhn

Involve/Concern

gio gap*

Iron (clothes)

reet

Island

gaw*


Hope this displays ok when sent but if not, the words in the third column
have different colours, font size and spacing.

Even if I select the word I want with the cursor, the code doesn't always
seem to run.

Any ideas please?

Thanks
 
H

Helmut Weber

Hi Dave,
this time more simple:
Note that .MatchWholeWord = True has it's drawbacks, too.
The problem is word's definition of "word".
Or the definition of "word" at all, but that's another story.
Don't forget to reset find options, if necessary.
Sub FindMe()

Dim sAny As String ' the string to search for
Dim iRow As Integer ' where am I
Dim iClm As Integer ' in order to be able to return
With Selection
sAny = .Text
iRow = .Information(wdEndOfRangeRowNumber)
iClm = .Information(wdEndOfRangeColumnNumber)
..SelectColumn
End With
With Selection.Find
.ClearFormatting
.Text = sAny
.MatchWholeWord = True
.MatchCase = True ' as you like
While .Execute
MsgBox "found"
Wend
' return to start, select word
Selection.Tables(1).Cell(iRow, iClm).Select
.Execute
End With
End Sub
 
D

Dave Neve

Hi

This time, the search doesn't stay in the column selected, yet '.select
column' is present in the code and before the search instructions

I've also tried replacing it with 'Selection.SelectColumn. cos I had seen
this previously.

How about a clue this time and I'll see if I can do it myself???


Thanks
 
H

Helmut Weber

Hi Dave,
the tricky thing is to offer help to a newbie, forgive me
for saying so, that can be understood. A perfect solution
is a pretty complicated matter.
First, it tried to do it theoretically well and avoided
"Selection.SelectColumn", as there is no column.range,
which may lead to complications, as I said, and here they are.
Word organizes tables in a linear way, that is from top left
to bottom right, no matter what you select. "SelectColumn" is a fake!
Later, I tried to keep it simple, hoping, that the word you
are searching for, would be in the very column only. No way!
That is the problem with simple solutions, they might work.
This time, the drawback would be multiple occurrences of the
same word in the same cell, as only the first occurrence should
be detected. If this fails, too, I must to come up with a d*mn*d
complicated thing. Just think of merged cells or nested tables.
Keep on! You're welcome.
---
Sub FindMe()
' information constants
' wdEndOfRangeRowNumber = 14
' wdEndOfRangeColumnNumber = 17
Dim i As Integer
Dim sAny As String ' the string to search for
Dim iRow As Integer
Dim iClm As Integer
With Selection
sAny = .Text
iRow = .Information(14)
iClm = .Information(17)
End With
For i = 1 To Selection.Tables(1).Rows.Count
Selection.Tables(1).Cell(i, 1).Select
With Selection.Find
.ClearFormatting
.Text = sAny
.MatchWholeWord = True
.MatchCase = True ' as you like
If .Execute Then
MsgBox "found"
End If
End With
Next
' return to start
Selection.Tables(1).Cell(iRow, iClm).Select
Selection.Find.Execute
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top