Look for text in a cell and then if found put other text in next c

N

Novice

Hey all, does anyone know of a way to look for "some text" in a cell in a
table, then move to the cell to the right of that cell and put "some other
text"?

I have the following - but it sometimes puts the "some other text" twice:
With ActiveDocument.Content.Find
.ClearFormatting
Do While .Execute(FindText:="some text", Forward:=True, _
Format:=True) = True
'Selection.Delete
Selection.SelectCell
Selection.MoveRight
'With .Parent
With .Parent
.StartOf Unit:=wdParagraph, Extend:=wdMove
.Move Unit:=wdCell, Count:=1
.InsertAfter "some other text"
.Move Unit:=wdCell, Count:=1
End With
Loop
End With

Also, ideally I would like to delete that "some text" first and then move to
the cell to the right and put "some other text". You can see where I have
deleted that portion of the code - because that just totally messed things up.

Thanks,
Novice
 
D

Doug Robbins

Try this:

Dim arange As Range, i As Long, j As Long
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="some text", Forward:=True) = True
Set arange = Selection.Range
i = arange.Information(wdEndOfRangeRowNumber)
j = arange.Information(wdEndOfRangeColumnNumber)
Selection.Tables(1).Cell(i, j + 1).Range.InsertAfter "Some other
text"
Selection.Tables(1).Cell(i, j).Range.Delete
Loop
End With

If "some text" is in the last column, it would need some modification to
insert "Some other text" in the first cell in the next row, but I am
assuming that is not the case.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 

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