Link to home
Start Free TrialLog in
Avatar of Alex Campbell
Alex CampbellFlag for United States of America

asked on

How to repeat a macro a certain number of times in Word 2010?

I have a macro to create a table and then add a couple of blank lines below it.
How would I adjust the macro to ask how many times it should be repeated and then it repeated that many times?

Here is the macro:
Sub CreateTable()
'
' CreateTable Macro
'
'
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=8, NumColumns:= _
        4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With
    Selection.EndKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.EndKey Unit:=wdStory
    Selection.TypeParagraph
    Selection.TypeParagraph
End Sub
Avatar of DrTribos
DrTribos
Flag of Australia image

Sub repeatN()
dim i as integer
For i = 1 to cint(inputbox "", "", "")
call CreateTable
next i
End Sub
I didn't test it - but input box will let you specify a prompt and title, and is a string.  The cInt (<string>)  part converts the string to an Integer.  It's not a very good solution but hope it helps.
Avatar of Alex Campbell

ASKER

Tried macro, but got error:  Compile Error: Syntax Error
Error-Repeat-Macro.jpg
ASKER CERTIFIED SOLUTION
Avatar of DrTribos
DrTribos
Flag of Australia image

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
Works great. Thanks