How to make a macro repeat itself?

G

greggorio

Hello,

I need to process a large table with a repeatable action. I have
recorded a macro and it works fine. The problem is, I need to make the
macro repeat itself until it reaches the end of the document. How to do
it?

The macro is:

----------------------------------

Sub kontr1()
'
' kontr1 Macro
' Macro recorded 2006-09-01 by greg
'
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.MoveLeft Unit:=wdCell
Selection.MoveLeft Unit:=wdCell
Selection.MoveLeft Unit:=wdCell
Selection.PasteAndFormat (wdPasteDefault)
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.Copy
Selection.MoveLeft Unit:=wdCell
Selection.MoveLeft Unit:=wdCell
Selection.MoveLeft Unit:=wdCell
Selection.MoveLeft Unit:=wdCell
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="-"
Selection.PasteAndFormat (wdPasteDefault)
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1

End Sub
 
J

Jezebel

Your macro appears to rely on the selection being at a particular place in
the table. This makes it very difficult to automate across the rest of the
document. You appear to be copying content between cells, so it's easy to
use a construction like

Dim pTable as Word.Table

For each pTable in ActiveDocument.Tables

pTable.Cells(r,c).range = ptable.Cells(r1,c1).range
:
Next

Where r and c are the row and column numbers
 
G

greggorio

Jezebel,

thanks a lot. This macro copies the contents of two cells in a row and
puts them into another cell. Then I want it to do the same action for
the next row, etc. till it reaches the end of the table.

Unfortunately, I'm a total newbie in VB. I don't even know, where to
place your code in my macro:). If I place it at the end of it (before
the End Sub statement), get a message "Compile error: Method or data
member not found". Or should I substitute anything for "r" and "c"?

Isn't there any easy way of telling the macro just "do the same again"?

Greg
 
J

Jezebel

As explained, you need to insert the row and column numbers for r and c.

There are lots of ways to make a macro repeat; but -- also as explained --
if your macro relies on the specific selection when you run it (as does
yours) it's not going to work if you run the macro when something else is
selected.

The short answer is, you need to learn a bit more about VBA.
 

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