Repeating VBA operations

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have several hundred lines of code that look like:

Sheets("First").Select
[LOTS OF CODE HERE]
End of code

If I want to repeat all of the same steps on two other sheets, do I have to
copy and paste the code twice under the names of the two different sheets or
is there a command I can use to repeat all of the code on different sheets?

Thanks

(P.S. I am a total novice, so the easier the solution the better.)
 
First step is declare a variable to hold the sheet names: I use one
called SheetName:

dim SheetName

Then set up a FOR...NEXT loop to repeat your code for each worksheet:

For each SheetName in Sheets
Sheets(SheetName).Select
[Lots of code here]
Next SheetName
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top