PDA

View Full Version : [SOLVED:] Adding Text to the end of a cell in a table



LoneReaper
01-11-2017, 05:57 PM
Good Morning all

I'm racking my brain here and I know the answer must be simple but I'm definitely missing something.

Essentially what I would like to do is to move to a specific cell in a table and add selected text at the end of whatever text is already written in there.

For example If Cell(4,2) had "ID, Company Name" written in it I would then like to update it to "ID, Company Name, Industry"

The code I have at the moment is as follows:

If CheckBox6.Value = True Then
ActiveDocument.Tables(1).Cell(4, 2).Select
Selection.MoveRight Count:=-1
Selection.TypeText Text:=", Industry"

I have tried variations of moveright & moveend as well as count being -1,0,1 and yet they all seem to still not be coming up correctly, either putting data into the previous cell (4,1) or deleting the text that is already in the cell and replacing it with the new text.

Any advice on this would be greatly appreciated.

Regards

William

gmayor
01-11-2017, 10:53 PM
This is easy to handle if you use ranges


Dim oCell As Range 'declare a range
Set oCell = ActiveDocument.Tables(1).Cell(4, 2).Range 'set the range to the cell in question
oCell.End = oCell.End - 1 'omit the cell end character from the range
oCell.Collapse 0 'collapse the range to its end
oCell.Text = ", Industry" 'enter the range text

LoneReaper
01-11-2017, 10:57 PM
Absolutely perfect! Thank you very much for that. I need to do more study / learning on declaring ranges I think.