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

Posted on 2016-11-08
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:= _
    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
End Sub
Question by:Alex Campbell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 15

Expert Comment

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

Expert Comment

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.

Author Comment

by:Alex Campbell
ID: 41881273
Tried macro, but got error:  Compile Error: Syntax Error
LVL 15

Accepted Solution

DrTribos earned 500 total points
ID: 41881342

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

Open in new window


Author Closing Comment

by:Alex Campbell
ID: 41885742
Works great. Thanks

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

623 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