Re-protect if originally protected, leave unprotected if not?

  • Thread starter StargateFanFromWork
  • Start date
S

StargateFanFromWork

I have this in my personal.xls for a button on one of my toolbars:



Sub FillDownWithNumberSeries()
'
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Trend:=False
End Sub



It's worked very well until now because I have never had protected sheets.
Now I have difficulty using if workbook is protected and am getting a little
tired of unprotecting all the time <g>. I've looked in the archives and
have found example code, but I'm not good at all on the ones dealing with IF
statements yet so have not yet had success in modifying the code above to
fit this situation.

To put at the beginning of the above "ActiveSheet.Unprotect" is not a
problem as this will work in all cases, I imagine, whether protected or not
(?). What I'm stuck on is for XL2K to re-protect (i.e., via something like
"ActiveSheet.Protect") only those workbooks and sheets that were protected
to begin with. How would one do this, pls?

Thank you! :eek:D
 
T

Tom Ogilvy

Assuming if anything on the sheet is protected, it will include content
protection.
Sub FillDownWithNumberSeries()
'
Dim bProtected as Boolean
bProtected = False
if activesheet.ProtectContents then
bProtected = True
ActiveSheet.Unprotect
end if
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Trend:=False
if bProtected then
Activesheet.Protect
end if
End Sub
 
S

StargateFanFromWork

Works perfectly!

I'll be able to modify all my other custom buttons now using the basics of
this code.

Thank you! :eek:D
 

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

Top