View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
StargateFan[_3_] StargateFan[_3_] is offline
external usenet poster
 
Posts: 171
Default Vital: Protecting sheets so macros/buttons still work?

On Thu, 1 Jul 2004 08:20:01 -0700, "Marcotte A"
wrote:

"StargateFan" wrote:

On Wed, 30 Jun 2004 16:19:34 -0400, "StargateFanFromWork"
Phew, okay, I'm feeling a little shaky, so if I may, I'm copying the above
macro with close to what I believe unprotect/protect code might be? p.s.,
to reiterate, I don't use passwords, it's not necessary; and, secondly, can
a "generic" worksheet name be used somehow? I have over a dozen sheets in
various workbooks to add the unprotect/protect code and it would be so much
easier to not have to name the sheets. This way, it'll be much easier to
quickly secure these documents from accidental modification of the layouts
and buttons.


[snip]

I'm not sure of syntax, so re-postiong.

Sub SortByFILENAME()
'
' Macro recorded 6/25/2004 by ...
'

'
ThisWorkbook.Worksheets().Unprotect()
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending,
header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
ThisWorkbook.Worksheets().Protect()
End Sub

************
I don't use passwords so the part where one enters a password, is that
code necessary? Also, all the sheets, as I mentioned above, have
different tab names. Is there a "generic" type of code for the sheet
name that will work?

Thanks.

You don't nee the "()" after Protect and Unprotect. As to doing multiple sheets, try:
Sub Sort()
Dim wks as Worksheet
For Each wks in ThisWorkbook
wks.Unprotect
Next wks

'your code

For Each wks in ThisWorkbook
wks.Protect
Next wks
End Sub


I'm sorry, I've confused the issue again. Very frustrating not
knowing terminology.

I only mentioned all the different sheets in the different workbooks
because I'm hoping to use the same code for all of them, copy/paste in
other words. I _don't_ need to reference them at all in the script.
The code above would be perfect but I'm just not sure of the syntax at
all. The original code calls for using an actual sheet name which is
no good for me. If the user changes the sheet name, I want the code
to still work yet I don't want to limit the user to not being able to
change the sheet name, if indeed such a thing would be possible.
Again, I _don't_ want to restrict this.

But, again, the code _does_not_ need to reference more than one sheet.
I don't have the documents here to test, I'l test at work later today
but here is the modified code:



Sub SortByFILENAME()
'
' Macro recorded 6/25/2004 by ...
'

'
ThisWorkbook.Worksheets.Unprotect
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending,
header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
ThisWorkbook.Worksheets.Protect

End Sub


Will this _really_ work on any workbook?

The only thing that would need changing for one or two sheets is the
"B2" reference, of course.. Most are B2, but I believe one or two,
due to an extra column, would be C2.

It just seems odd that the () aren't needed, but hey, what do I know??
<g

Thanks!