ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Repeating VBA operations (https://www.excelbanter.com/excel-discussion-misc-queries/15499-repeating-vba-operations.html)

Adam1 Chicago

Repeating VBA operations
 
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.)

Dave O

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


Adam1 Chicago

thanks again!

"Dave O" wrote:

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



Dave O

Glad to help!



All times are GMT +1. The time now is 03:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com