Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-11-08
5
Medium Priority
?
261 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:Alex Campbell
  • 3
  • 2
5 Comments
 
LVL 15

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 15

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
 
LVL 1

Author Comment

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

Accepted Solution

by:
DrTribos earned 2000 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
 
LVL 1

Author Closing Comment

by:Alex Campbell
ID: 41885742
Works great. Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

876 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