Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 454
Default Protect via Code Q

To password protect a sheet I use a very simple piece of code, as
below, but how do I incorporate code to ensure that the "Select
Unlocked Cells" is UNCHECKED when protecting


Sub Protect()

Sheets("Sheet1").Select
ActiveSheet.Protect Password:="1234"

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Protect via Code Q

Sean,
Try something like

Dim WS As Worksheet
Set WS = Worksheets("Sheet1")
WS.EnableSelection = xlNoSelection
WS.Protect Password:="1234"

I'll give you a piece of advice that will serve you well as you get deeper
in to Excel and VBA: Get out of the habit now, not later, of using Select in
VBA. It is (almost) never necessary to Select anything. Instead, reference a
worksheet or workbook or range or whatever directly. Don't Select anything.
Your code will be cleaner, faster, and easier to maintain.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Sean" wrote in message
ups.com...
To password protect a sheet I use a very simple piece of code, as
below, but how do I incorporate code to ensure that the "Select
Unlocked Cells" is UNCHECKED when protecting


Sub Protect()

Sheets("Sheet1").Select
ActiveSheet.Protect Password:="1234"

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 454
Default Protect via Code Q

Thanks Guys

Chip I assume then if I ever want to "select" something again I simple
preface my code with

Dim WS As Worksheet
Set WS = Worksheets("'whatever")
WS. "whatever"

Is there any good sites for novice vba writers? Particularly a listing
of what things relate to (from what I see on Excel) eg. xlNoSelection
etc or do I just record a macro and work it out that way. I can get
things working eventually with some guidance, but most of how the code
works pass right over me.



Chip Pearson wrote:
Sean,
Try something like

Dim WS As Worksheet
Set WS = Worksheets("Sheet1")
WS.EnableSelection = xlNoSelection
WS.Protect Password:="1234"

I'll give you a piece of advice that will serve you well as you get deeper
in to Excel and VBA: Get out of the habit now, not later, of using Select in
VBA. It is (almost) never necessary to Select anything. Instead, reference a
worksheet or workbook or range or whatever directly. Don't Select anything.
Your code will be cleaner, faster, and easier to maintain.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Sean" wrote in message
ups.com...
To password protect a sheet I use a very simple piece of code, as
below, but how do I incorporate code to ensure that the "Select
Unlocked Cells" is UNCHECKED when protecting


Sub Protect()

Sheets("Sheet1").Select
ActiveSheet.Protect Password:="1234"

Thanks


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Protect via Code Q

Sean,

Set WS = Worksheets("'whatever")
WS. "whatever"


No. The WS variable already refers to the sheet named "whatever". That what
the Set statement does. Once have the WS variable pointing to the
appropriate sheet, use WS where you would normally use ActiveSheet or
Worksheets("Whatever"). E.g.,

Set WS = Worksheets("'whatever")
WS.Range("A1").Value = 1234

This puts the value 1234 in cell A1 of whatever sheet WS is refering to.

Recording a macro is the easiest way to see what properties and methods are
used, but the code it produces is not pretty bad (mainly because it cannot
anticipate what you are going to do in the next step). It uses Select and
Selection. Use the methods (e..g, Sort) or properties (e.g, Bold), but
create variables for the objects in question. E.g., rather than

Range("A1").Select
Selection.Font.Bold = True

use code like

Range("A1").Font.Bold = True

or

Dim Rng As Range
Set Rng = Range("A1")
Rng.Font.Bold = True



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Sean" wrote in message
oups.com...
Thanks Guys

Chip I assume then if I ever want to "select" something again I simple
preface my code with

Dim WS As Worksheet
Set WS = Worksheets("'whatever")
WS. "whatever"

Is there any good sites for novice vba writers? Particularly a listing
of what things relate to (from what I see on Excel) eg. xlNoSelection
etc or do I just record a macro and work it out that way. I can get
things working eventually with some guidance, but most of how the code
works pass right over me.



Chip Pearson wrote:
Sean,
Try something like

Dim WS As Worksheet
Set WS = Worksheets("Sheet1")
WS.EnableSelection = xlNoSelection
WS.Protect Password:="1234"

I'll give you a piece of advice that will serve you well as you get
deeper
in to Excel and VBA: Get out of the habit now, not later, of using Select
in
VBA. It is (almost) never necessary to Select anything. Instead,
reference a
worksheet or workbook or range or whatever directly. Don't Select
anything.
Your code will be cleaner, faster, and easier to maintain.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Sean" wrote in message
ups.com...
To password protect a sheet I use a very simple piece of code, as
below, but how do I incorporate code to ensure that the "Select
Unlocked Cells" is UNCHECKED when protecting


Sub Protect()

Sheets("Sheet1").Select
ActiveSheet.Protect Password:="1234"

Thanks




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
Can protect worksheet then workbook but not Protect and Share in code [email protected] Excel Programming 7 January 16th 17 07:01 AM
Protect Sheet with code, but then code will not Paste error. How do i get around this. Please read for explainations.... Corey Excel Programming 4 November 25th 06 04:57 AM
Generic protect/unprotect code through buttons and code? StargateFanFromWork[_3_] Excel Programming 4 December 31st 05 12:10 AM
What is the best way to protect VBA code Daniel CHEN Excel Programming 2 July 19th 05 08:52 AM
Protect Code Mike Excel Programming 4 January 5th 04 01:48 AM


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