copy last row of a table to a new row

D

Drew

I have a table of data that has tokan names in each cell to be replaced with
real data using VBA from another application. I don't know how many rows I'm
going to end up with so it has to be dynamic. I start with 2 rows, one with
headings and the second with the tokens. I need to copy the 2nd row to the
3rd, replace the tokens in row 2 with data, move to the last row and repeat
until I run out of data.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RHJldw==?=,
I have a table of data that has tokan names in each cell to be replaced with
real data using VBA from another application. I don't know how many rows I'm
going to end up with so it has to be dynamic. I start with 2 rows, one with
headings and the second with the tokens. I need to copy the 2nd row to the
3rd, replace the tokens in row 2 with data, move to the last row and repeat
until I run out of data.
There's certainly more than one way to do this. Here's one of them. Note that
I've just used a simple counter, rather than looping through a data set, so
you'll need to change the top-level For...Next to fit your scenario.

Sub DuplicateTableRow()
Dim lstart As Long
Dim lend As Long
Dim counter As Long
Dim cellCounter As Long
Dim tbl As Word.Table
Dim rwOrig As Word.Row
Dim rwNew As Word.Row
Dim rngOrig As Word.Range
Dim rngNew As Word.Range

lstart = 1
lend = 3
Set tbl = ActiveDocument.Tables(1)
Set rwOrig = tbl.Rows(2)
For counter = lstart To lend
Set rwNew = tbl.Rows.Add
For cellCounter = 1 To rwOrig.Cells.Count
Set rngOrig = rwOrig.Cells(cellCounter).Range
rngOrig.MoveEnd Unit:=wdCharacter, Count:=-1
Set rngNew = rwNew.Cells(cellCounter).Range
rngNew.Collapse wdCollapseStart
rngNew.FormattedText = rngOrig.FormattedText
Next
Next
rwOrig.Delete
End Sub


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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