Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Protect a single worksheet?


I am using Excel 97.

The reason I am asking this question is because I have a Worksheet
that has code in the Worksheet_Change(). Each time the user changes
data in this worksheet the code does stuff to validate the data. A lot
of stuff. So I wrote code to do the checking. The problem is if the
user deletes the Worksheet my code is also deleted. If they rename the
sheet, my code stops working.

I tried to protect a an Excel Spreadsheet. I used Tools menu
Protection Protect Workbook. Then I picked protect structure.

Protecting the structure was not good because it protects everything.
I just want to protect the sheet with my code. The user can do
whatever they want to all the other worksheets.

Is there a way to do this.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default Protect a single worksheet?

Nigel

Protecting the sheet will not prevent deletion of that sheet.

This event code placed in Thisworkbook module will prevent the sheet being
deleted or re-named.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If ActiveSheet.Name = "MySheet" Or _
ActiveWindow.SelectedSheets.Count 1 Then
ThisWorkbook.Protect Password:="justme", Structu=True
Else
ThisWorkbook.Unprotect Password:="justme"
End If
End Sub

Note: also prevents any grouped sheets from being deleted/copied/moved but
OP can probably live with that.

I will question OP on why the sheetname is hard-coded in the
Worksheet_Change event code?

To prevent users from seeing the password, lock the VBAProject to prevent
viewing the code.


Gord Dibben MS Excel MVP


On Sat, 20 Dec 2008 16:42:35 -0000, "Nigel"
wrote:

I presume when you say protect you mean the name and deletion of the sheet?

You can protect the sheet having first made the cells you want the use to
edit unlocked.

Instead of using the sheet name (as per tab) use the sheet codename, that
way your users can change tab names without affecting your code.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Protect a single worksheet?

On Dec 20, 1:23*pm, Gord Dibben <gorddibbATshawDOTca wrote:
Nigel

Protecting the sheet will not prevent deletion of that sheet.

This event code placed in Thisworkbook module will prevent the sheet being
deleted or re-named.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If ActiveSheet.Name = "MySheet" Or _
* * * *ActiveWindow.SelectedSheets.Count 1 Then
* * * * * * *ThisWorkbook.Protect Password:="justme", Structu=True
Else
* * * * * * *ThisWorkbook.Unprotect Password:="justme"
End If
End Sub

Note: also prevents any grouped sheets from being deleted/copied/moved but
OP can probably live with that.

I will question OP on why the sheetname is hard-coded in the
Worksheet_Change event code?

To prevent users from seeing the password, lock the VBAProject to prevent
viewing the code.

Gord Dibben *MS Excel MVP

On Sat, 20 Dec 2008 16:42:35 -0000, "Nigel"
wrote:

I presume when you say protect you mean the name and deletion of the sheet?


You can protect the sheet having first made the cells you want the use to
edit unlocked.


Instead of using the sheet name (as per tab) use the sheet codename, that
way your users can change tab names without affecting your code.


Thanks for the reply everyone.

So I saw a question aimed at me. Why hardcode the sheet name?

I wrote some code that deletes rows from the worksheet. I noticed that
if I clicked around, the wrong rows was deleted. But when I did
something like

Sheets("MySheet").select
Delete rows here . . .

By selecting the sheet, I was sure that the rows deleted was in the
right worksheet. Not just what happened to be in focus at the time.

Is this not a good idea?
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default Protect a single worksheet?

Thanks Gord for pointing out the need to protect the workbook structure as
well.

As far as the OP referencing the worksheet, it is true that to fully
reference the sheet prevents unexpected changes on other sheets and
depending on how the name is protected, using Sheets("MySheet") does rely on
the sheet being called "MySheet"; once that is changed the code will fail.

Using a sheet codename; which can only be changed in the VBE project window
or by using the default values of Sheet1, Sheet2 etc, will overcome this
limitation.

You may have noticed in the VBE project window that sheets are named e.g.
Sheet1(Sheet1) etc. You can change the codename by clicking on the sheet
in the project list and press F4. The properties are shown, (Name) is the
codename; Name is the tab name. So to select a sheet named MySheet or the
sheet codenamed Sheet1 use

Sheets("MySheet").Select

Sheet1.Select


--

Regards,
Nigel




"General Fear" wrote in message
...
On Dec 20, 1:23 pm, Gord Dibben <gorddibbATshawDOTca wrote:
Nigel

Protecting the sheet will not prevent deletion of that sheet.

This event code placed in Thisworkbook module will prevent the sheet being
deleted or re-named.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If ActiveSheet.Name = "MySheet" Or _
ActiveWindow.SelectedSheets.Count 1 Then
ThisWorkbook.Protect Password:="justme", Structu=True
Else
ThisWorkbook.Unprotect Password:="justme"
End If
End Sub

Note: also prevents any grouped sheets from being deleted/copied/moved but
OP can probably live with that.

I will question OP on why the sheetname is hard-coded in the
Worksheet_Change event code?

To prevent users from seeing the password, lock the VBAProject to prevent
viewing the code.

Gord Dibben MS Excel MVP

On Sat, 20 Dec 2008 16:42:35 -0000, "Nigel"
wrote:

I presume when you say protect you mean the name and deletion of the
sheet?


You can protect the sheet having first made the cells you want the use to
edit unlocked.


Instead of using the sheet name (as per tab) use the sheet codename, that
way your users can change tab names without affecting your code.


Thanks for the reply everyone.

So I saw a question aimed at me. Why hardcode the sheet name?

I wrote some code that deletes rows from the worksheet. I noticed that
if I clicked around, the wrong rows was deleted. But when I did
something like

Sheets("MySheet").select
Delete rows here . . .

By selecting the sheet, I was sure that the rows deleted was in the
right worksheet. Not just what happened to be in focus at the time.

Is this not a good idea?



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default Protect a single worksheet?

You do have to make sure you are working on the proper sheet so pointing to
that sheet would be a necessity if you are running the macro while another
sheet has the focus.

Your code does that but a slight revision could be made.

Instead of selecting "MySheet" you can use

With Sheets("MySheet")

Do your stuff

End With

To prevent the deletion or rename of that sheet try the code I posted.


Gord

On Sat, 20 Dec 2008 17:20:03 -0800 (PST), General Fear
wrote:

On Dec 20, 1:23*pm, Gord Dibben <gorddibbATshawDOTca wrote:
Nigel

Protecting the sheet will not prevent deletion of that sheet.

This event code placed in Thisworkbook module will prevent the sheet being
deleted or re-named.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If ActiveSheet.Name = "MySheet" Or _
* * * *ActiveWindow.SelectedSheets.Count 1 Then
* * * * * * *ThisWorkbook.Protect Password:="justme", Structu=True
Else
* * * * * * *ThisWorkbook.Unprotect Password:="justme"
End If
End Sub

Note: also prevents any grouped sheets from being deleted/copied/moved but
OP can probably live with that.

I will question OP on why the sheetname is hard-coded in the
Worksheet_Change event code?

To prevent users from seeing the password, lock the VBAProject to prevent
viewing the code.

Gord Dibben *MS Excel MVP

On Sat, 20 Dec 2008 16:42:35 -0000, "Nigel"
wrote:

I presume when you say protect you mean the name and deletion of the sheet?


You can protect the sheet having first made the cells you want the use to
edit unlocked.


Instead of using the sheet name (as per tab) use the sheet codename, that
way your users can change tab names without affecting your code.


Thanks for the reply everyone.

So I saw a question aimed at me. Why hardcode the sheet name?

I wrote some code that deletes rows from the worksheet. I noticed that
if I clicked around, the wrong rows was deleted. But when I did
something like

Sheets("MySheet").select
Delete rows here . . .

By selecting the sheet, I was sure that the rows deleted was in the
right worksheet. Not just what happened to be in focus at the time.

Is this not a good idea?


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default Protect a single worksheet?

I do understand what codenames are and how they are used.

Using the codename is OK if all you want to do is point to the same sheet if
it re-named but not much good if the sheet is deleted.

OP wants to prevent a sheet delete hence my posted code which prevents
deletion or rename of "MySheet"


Gord

On Sun, 21 Dec 2008 02:57:27 -0000, "Nigel"
wrote:

Thanks Gord for pointing out the need to protect the workbook structure as
well.

As far as the OP referencing the worksheet, it is true that to fully
reference the sheet prevents unexpected changes on other sheets and
depending on how the name is protected, using Sheets("MySheet") does rely on
the sheet being called "MySheet"; once that is changed the code will fail.

Using a sheet codename; which can only be changed in the VBE project window
or by using the default values of Sheet1, Sheet2 etc, will overcome this
limitation.

You may have noticed in the VBE project window that sheets are named e.g.
Sheet1(Sheet1) etc. You can change the codename by clicking on the sheet
in the project list and press F4. The properties are shown, (Name) is the
codename; Name is the tab name. So to select a sheet named MySheet or the
sheet codenamed Sheet1 use

Sheets("MySheet").Select

Sheet1.Select


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
how do I protect a single cell JimK Excel Discussion (Misc queries) 2 November 11th 08 10:16 PM
How do I protect one single cell? Neil Grafton New Users to Excel 3 July 5th 08 09:29 PM
How can I password protect a single worksheet within a workbook Akash Excel Programming 1 July 3rd 07 07:04 AM
How do I password protect a single worksheet within a workbook f Lorne Excel Discussion (Misc queries) 1 July 3rd 07 05:21 AM
How do I password protect a single tab in a worksheet in excel ? Fritz Excel Discussion (Misc queries) 4 March 6th 06 02:30 PM


All times are GMT +1. The time now is 04:44 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"