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

Microsoft WordVisual Basic Classic

Avatar of undefined
Last Comment
Fi69

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
dotoffice

use :

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


myCell.select
Fi69

ASKER
it errors on this line
SET myCell =  ActiveDocument.Tables(2).Cell(" & myrange.Cells(1).RowIndex & ", " & myrange.Cells(1).ColumnIndex & ")
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dotoffice

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
Fi69

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

ASKER
Exactly what I was looking for.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
GrahamSkan

As dotoffice has illustrated above, if you only want to select the cell, you don't need to know the address.
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.