Table - moving rows up and down...

M

Mads W.

I have a table where lines are merged in from a ERP program.
The merge is done with bookmarks, and is not using the standard mergefields
in Word.

When merging Orderlines from the ERP, it uses a table. The table looks like
this:
(Tab shows new Colum, new line shows new Row)

---Table---
Headlinie Headline Headline Headline
Linetxt Amount Price Totalamount
Linenote empty empty empty
Linetxt Amount Price Totalamount
Linetxt Amount Price Totalamount
Linenote empty empty empty
....
---Table end---

I want to write some code that runs through the table and puts the Linenote
row over the Linetxt row.
But there should be a check on the 2. colum in the Linenote row. If the
field isn't empty no switch should be made, but the next rows should be
checked.

My result should be this:

---Table---
Headlinie Headline Headline Headline
Linenote empty empty empty
Linetxt Amount Price Totalamount
Linetxt Amount Price Totalamount
Linenote empty empty empty
Linetxt Amount Price Totalamount
....
---Table end---

I can put a bookmark in the first headline cell of the table, so that can be
used as start point.
 
H

Helmut Weber

Hi,
many questions at once.
How do you identify rows at all?
Is there a text "linetxt" or "linenote" in the first cell?
Must formatting taken care of, too?

Simple example for moving a row including formatting,
move row 30 to row 25:

Sub test907()
MoveRow 30, 25
End Sub

Sub MoveRow(RowF As Long, RowT As Long)
' rowF = from
' rowT = to
With ActiveDocument.Tables(1)
.Rows(RowF).Range.Copy
.Rows(RowT).Range.Paste
.Rows(RowF + 1).Range.Cut
End With

End Sub

There are many other possible methods.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
M

mads.westen

Hi Helmut,

I have a bookmark "Tableflag" at the first Row of the table. That row
is a headline row, and should not be moved.

I don't have that good identification of my rown. However, the rows are
merged in pairs. If there is a Linenote row, it will appear under the
Linetxtrow. The Linenote row has no data in 2. colum. Linetxtrows
always have data in 2. colum.

Formatting is not an issue, all rows have the same formatting.

I'm trying to use your pasted code (thx btw). I need some code that
will run through the table and stop when at the buttom. How can I
identify what row I'm in?
 
H

Helmut Weber

Hi,
nice little project,
MsgBox Selection.Information(wdEndOfRangeRowNumber)
tells you what row the end of the selection is in.
But you might not need that information at all.

Sub Test4091()
Dim oTbl As Table
Dim r As Long
Set oTbl = ActiveDocument.Tables(1)
For r = oTbl.Rows.Count To 2 Step -1
' process table backwards, exclude row 1
oTbl.Rows(r).Select ' for testing only
If Len(oTbl.Range.Rows(r).Cells(2).Range) = 2 Then
MsgBox "row " & r & " is a linenote row"
' as cell 2 in the row is empty
End If
Next
End Sub

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
M

Mads W.

Hi again,

Helmut - you're the man :)
I must say that you helped my all the way!
Thank you very much for that.

Now I'll paste my final code, which you can see, is a product of your two
posts. I'm posting it all if anybody should have interrest in using it later
on.

Here goes:

Sub Tablerun()
Dim oTbl As Table
Dim r As Long

If ActiveDocument.Bookmarks.Exists("Tabelflag") = True Then
Selection.GoTo What:=wdGoToBookmark, Name:="Tabelflag"
' The document holds more than one table, so here we decide
where to go
End If

Set oTbl = Selection.Tables(1)
For r = oTbl.Rows.Count To 2 Step -1
' process table backwards, exclude row 1
oTbl.Rows(r).Select ' for testing only
If Len(oTbl.Range.Rows(r).Cells(2).Range) = 2 Then
Call MoveRow(r, r - 1)
r = r - 1 'We don't want to move the row we just moved once
again
' Linenote row as cell 2 in the row is empty
End If
Next
End Sub

Public Function MoveRow(RowF As Long, RowT As Long)
' rowF = from
' rowT = to
'MsgBox "Test af function"
With Selection.Tables(1)
.Rows(RowF).Range.Copy
.Rows(RowT).Range.Paste
.Rows(RowF + 1).Range.Cut
End With

End Function

Best regards
Mads W.
 

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