Solved

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

Posted on 2016-11-08
5
34 Views
Last Modified: 2016-11-13
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
0
Comment
Question by:Alex972
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:DrTribos
ID: 41879769
Sub repeatN()
dim i as integer
For i = 1 to cint(inputbox "", "", "")
call CreateTable
next i
End Sub
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 41879773
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.
0
 

Author Comment

by:Alex972
ID: 41881273
Tried macro, but got error:  Compile Error: Syntax Error
Error-Repeat-Macro.jpg
0
 
LVL 14

Accepted Solution

by:
DrTribos earned 500 total points
ID: 41881342
Try:

Sub repeatN()
Dim i As Integer
For i = 1 To CInt(InputBox("", "", ""))
Call CreateTable
Next i
End Sub

Open in new window

0
 

Author Closing Comment

by:Alex972
ID: 41885742
Works great. Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now