Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Phil Osman
 
Posts: n/a
Default Repeat a command in a Macro

I have a macro that simply looks like this:

Sheets("Template").Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
etc................

I have 72 sheets, and don't want to keep having to go in and paste in more
lines everytime I add a new sheet.
Is there someway to tell it to repeat the Select & Unprotect lines x number
of times, which would give a MUCH shorter code! Or, maybe there is a way to
tell it to keep repeating that until it gets to the last sheet in the
workbook ???

Phil

--
http://www.redbrick.dcu.ie/~pele
  #2   Report Post  
KL
 
Posts: n/a
Default

Hi Phil,

You can use loops like this one:

For each ws in ActiveWorkbook.Worksheets
ws.Unprotect
Next ws

Regards,
KL


"Phil Osman" wrote in message
...
I have a macro that simply looks like this:

Sheets("Template").Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
etc................

I have 72 sheets, and don't want to keep having to go in and paste in more
lines everytime I add a new sheet.
Is there someway to tell it to repeat the Select & Unprotect lines x
number
of times, which would give a MUCH shorter code! Or, maybe there is a way
to
tell it to keep repeating that until it gets to the last sheet in the
workbook ???

Phil

--
http://www.redbrick.dcu.ie/~pele



  #3   Report Post  
Phil Osman
 
Posts: n/a
Default

That works, but is there a way to modify the code so that it only starts from
SheetX and continues to the end of the workbook ?

--
http://www.redbrick.dcu.ie/~pele


"KL" wrote:

Hi Phil,

You can use loops like this one:

For each ws in ActiveWorkbook.Worksheets
ws.Unprotect
Next ws

Regards,
KL


"Phil Osman" wrote in message
...
I have a macro that simply looks like this:

Sheets("Template").Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
etc................

I have 72 sheets, and don't want to keep having to go in and paste in more
lines everytime I add a new sheet.
Is there someway to tell it to repeat the Select & Unprotect lines x
number
of times, which would give a MUCH shorter code! Or, maybe there is a way
to
tell it to keep repeating that until it gets to the last sheet in the
workbook ???

Phil

--
http://www.redbrick.dcu.ie/~pele




  #4   Report Post  
KL
 
Posts: n/a
Default

Try this:

For i=5 To Worksheets.Count
Worksheets(i).Unprotect
Next i

KL

"Phil Osman" wrote in message
...
That works, but is there a way to modify the code so that it only starts
from
SheetX and continues to the end of the workbook ?

--
http://www.redbrick.dcu.ie/~pele


"KL" wrote:

Hi Phil,

You can use loops like this one:

For each ws in ActiveWorkbook.Worksheets
ws.Unprotect
Next ws

Regards,
KL


"Phil Osman" wrote in message
...
I have a macro that simply looks like this:

Sheets("Template").Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
etc................

I have 72 sheets, and don't want to keep having to go in and paste in
more
lines everytime I add a new sheet.
Is there someway to tell it to repeat the Select & Unprotect lines x
number
of times, which would give a MUCH shorter code! Or, maybe there is a
way
to
tell it to keep repeating that until it gets to the last sheet in the
workbook ???

Phil

--
http://www.redbrick.dcu.ie/~pele






  #5   Report Post  
Phil Osman
 
Posts: n/a
Default

I changed the i = 5 to i = 12, presuming that is the sheet that will start
getting unprotected from which seems to work.

LASTLY (!!): How can I get this to loop from Sheet 12 to the end:
ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingRows:=True,
AllowFiltering:=True

--
http://www.redbrick.dcu.ie/~pele


"KL" wrote:

Try this:

For i=5 To Worksheets.Count
Worksheets(i).Unprotect
Next i

KL

"Phil Osman" wrote in message
...
That works, but is there a way to modify the code so that it only starts
from
SheetX and continues to the end of the workbook ?

--
http://www.redbrick.dcu.ie/~pele


"KL" wrote:

Hi Phil,

You can use loops like this one:

For each ws in ActiveWorkbook.Worksheets
ws.Unprotect
Next ws

Regards,
KL


"Phil Osman" wrote in message
...
I have a macro that simply looks like this:

Sheets("Template").Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
ActiveSheet.Next.Select
ActiveSheet.Unprotect
etc................

I have 72 sheets, and don't want to keep having to go in and paste in
more
lines everytime I add a new sheet.
Is there someway to tell it to repeat the Select & Unprotect lines x
number
of times, which would give a MUCH shorter code! Or, maybe there is a
way
to
tell it to keep repeating that until it gets to the last sheet in the
workbook ???

Phil

--
http://www.redbrick.dcu.ie/~pele






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
repeat macro formula to all cells dave glynn Excel Discussion (Misc queries) 8 March 10th 05 08:42 PM
Playing a macro from another workbook Jim Excel Discussion (Misc queries) 1 February 23rd 05 10:12 PM
Macro to repeat formulas in next row katiekay Excel Discussion (Misc queries) 5 February 10th 05 08:28 PM
Date macro Hiking Excel Discussion (Misc queries) 9 February 3rd 05 12:40 AM
How to program an excel macro to repeat a series of keystrokes? Beancounter Excel Discussion (Misc queries) 8 January 22nd 05 11:51 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"