Link to home
Start Free TrialLog in
Avatar of Andreas Hermle
Andreas HermleFlag for Germany

asked on

Code snippet to grab a cell content of a table cell using VBA

Dear Experts:

I would like to run a macro that performs the following task:

Go the very first table of the active document and copy the contents of the following cell into the clipboard: Column 4 and Row 1. After copying the cell contents into the clipboard delete the contents of that cell.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Avatar of Shums Faruk
Shums Faruk
Flag of India image

For copying and clearing you can use below code:
Sub Sample()
Dim Tbl_1 As Table
Set Tbl_1 = ActiveDocument.Tables(1)
Tbl_1.Cell(1, 4).Range.Copy
Tbl_1.Cell(1, 4).Range.Delete
End Sub

Open in new window

Do you want to paste somewhere?
Avatar of Rgonzo1971
Rgonzo1971

HI,

If you just want the content of the cell and not the cell then try
Sub Sample()
With ActiveDocument.Tables(1).Cell(1, 4)
    Set rng = ActiveDocument.Range(.Range.Start, .Range.End - 1)
    rng.Copy
    .Range.Delete
End With
End Sub

Open in new window

Regards
Avatar of Andreas Hermle

ASKER

Dear both,

both codes work very  fine. Thank you very much for it. Rgonzo, thank you for that specific feature.

In the meantime,  the requirement of the macro code has changed.

Cell 4 should not be hard coded, but the last cell in row 1 of the very first table is always the cell where the macro is to work on (i.e. the tables vary in the number of columns). Would be great if either of you could change the code accordingly.

Thank you very much.

Regards, Andreas
Shums, thank you for your 'pasting' question. It is not relevant for the moment. Thank you, Regards, Andreas
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

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
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
Dear both, thank you very much for your professional help. I really appreciate it.

It is always hard to award points since both codes work just fine, but Rgonzo is more concise and therefore the majority of the points go to him. I hope you understand Shums :-)

Regards, Andreas