Alex Campbell
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:=wdWo rd9TableBe havior, 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
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:=wdWo
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
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.
ASKER
Tried macro, but got error: Compile Error: Syntax Error
Error-Repeat-Macro.jpg
Error-Repeat-Macro.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works great. Thanks
dim i as integer
For i = 1 to cint(inputbox "", "", "")
call CreateTable
next i
End Sub