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
VBAMicrosoft WordMicrosoft Office

Avatar of undefined
Last Comment
Andreas Hermle
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
Avatar of Andreas Hermle

ASKER

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

Blurred text
THIS SOLUTION IS 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
SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Blurred text
THIS SOLUTION IS 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.
Avatar of Andreas Hermle

ASKER

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
Microsoft Office
Microsoft Office

Microsoft Office is an integrated suite of applications that includes Outlook, Word, Excel, Access, PowerPoint, Visio and InfoPath, along with a number of tools to assist in making the individual components work together. Coding within and between the projects is done in Visual Basic for Applications, known as VBA.

80K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo