Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop macro from userform

Status
Not open for further replies.

glyrocks

Technical User
Nov 16, 2006
57
0
0
US
I've got a handful of macros in Word 2003 that format table rows. I can get them to loop x number of times no problem, but only if I hard code it in the macro. What I had envisioned was a userform popping up that would ask how many times to run the macro, user enters a number in a text box that is then passed to the macro. However, the loop number the user enters isn't being passed to the macro. I guess the variable value is lost when the form is closed? Thanks for any help!

dylan
 




Hi,

You need to become familiar with the Word Object Model. You could do something like this ...
Code:
dim iRowCnt as integer, iColCnt as integer
with thisdocument.tables("Table1")
  iRowCnt = .Rows.Count
  iColCnt = .Columns.Count
end with


Skip,
[sub]
[glasses] When a wee mystic is on the loose..
It's a Small Medium at Large! [tongue][/sub]
 
What is the logic of your code (macro creates userform vs. userform calls the macro) and how do you transfer this number to the macro?

combo
 
Thanks for the quick responses but I found a solution. I was trying to declare a variable in the form, and then use that variable in the macro. Instead, I just pulled the variable from within the macro: rows = UserForm1.TextBox1.Value

thanks again!

dylan
 
What if the user enters the wrong number?

It won't be difficult to have the macro count how many rows there are to ensure that they are all looped through.

Backing up even further, what are you doing that requires each row to be looped through? Are you sure that you can simply format the entire table at once?

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
It isn't looping through each row, rather repeating a set of formatting instructions.

The table runs anywhere from 20 to 150 pages so I needed a way for users to tell the macro how many pages to format. Counting the number of rows could be useful though.

dylan
 
Is it all one continious table ?

and have you looked at the methods availiable on the document object ?

Chance,

F, G + B
 
It is all one continuous table, but I don't want to format it all at once. There are a couple of nuances with the data and formatting that the user needs to watch for. So, by giving them a form to specify how many times to repeat something, they can stop and adjust things as needed.

dylan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top