Link to home
Start Free TrialLog in
Avatar of Fi69
Fi69

asked on

Word vba code to return the position of the cell I'm in

Hi experts
I suspect this is probably simple, but I have no idea how to do it, or go about it.

I've selected a particular table and I want to find particular text in there. Then I want to go to that text and return the value of the cell I'm in, for example Row(4), Cell(2).

Can someone tell me how to do this.
Dim myrange As Range
set myrange = ActiveDocument.Tables(2).Range
            
  myrange.Find.Execute findtext:=texta
                        
   If myrange.Find.Found = True Then
   'goto that text. What cell am I in?
   End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fi69
Fi69

ASKER

Hi Graham
Thank you the message box tells me what cell I need correctly. That's fantastic.

Sorry, novice here, having trouble selecting that cell. Do you know what I'm doing wrong?

Dim myCell as 'I can't get anything to work'
myCell = ActiveDocument.Tables(2).Cell(" & myrange.Cells(1).RowIndex & ", " & myrange.Cells(1).ColumnIndex & ")
              myCell.range.Select
use :

dim myCell as cell
SET myCell =  ActiveDocument.Tables(2).Cell(" & myrange.Cells(1).RowIndex & ", " & myrange.Cells(1).ColumnIndex & ")


myCell.select
Avatar of Fi69

ASKER

it errors on this line
SET myCell =  ActiveDocument.Tables(2).Cell(" & myrange.Cells(1).RowIndex & ", " & myrange.Cells(1).ColumnIndex & ")
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dim myrange As Range
Set myrange = ActiveDocument.Tables(2).Range
             
myrange.Find.Execute findtext:=texta
                         
If myrange.Find.Found = True Then
  myrange.Cells(1).Select
End If
Avatar of Fi69

ASKER

That worked - gosh I frustrate myself. Thanks Graham!
Avatar of Fi69

ASKER

Exactly what I was looking for.
As dotoffice has illustrated above, if you only want to select the cell, you don't need to know the address.
Avatar of Fi69

ASKER

Yes, that is a much more simple way of doing what I've ended up doing. Sorry dotoffice I already awarded points. Thanks for your help! I ended up changing what I originally set out to do.