MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 7th, 2002, 09:16 AM   #1
jscott
New Member
 
Join Date: Mar 2002
Posts: 5
Default

I'm trying to protect a certain range of cells in a worksheet from within Access.

My code is below:

Dim objExcel As Excel.Application
Dim objWB As Excel.Workbook
Dim objWS As Worksheet
Dim intColumn As Integer
Dim intRow As Integer
Dim intRecords As Integer

Set objExcel = Excel.Application
objWS.Unprotect
objWS.Protection.AllowEditRanges.Add Title:="Classified", Range:=Range("$A$2:$D$65536"), Password:=""

objWS.Columns.AutoFit
objWS.Protect
objWB.SaveAs strFileName

I get a method or data member not found on the objws.protection line.

I am referencing the Excel 8.0 object library.

Can anyone help?? I'd really appreciate it. I'm new to the Excel object model!

Thanks!
Jeanine Scott
jscott is offline   Reply With Quote
Old Mar 7th, 2002, 09:25 AM   #2
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Quote:
On 2002-03-07 08:16, jscott wrote:
I'm trying to protect a certain range of cells in a worksheet from within Access.

My code is below:

Dim objExcel As Excel.Application
Dim objWB As Excel.Workbook
Dim objWS As Worksheet
Dim intColumn As Integer
Dim intRow As Integer
Dim intRecords As Integer

Set objExcel = Excel.Application
objWS.Unprotect
objWS.Protection.AllowEditRanges.Add Title:="Classified", Range:=Range("$A$2:$D$65536"), Password:=""

objWS.Columns.AutoFit
objWS.Protect
objWB.SaveAs strFileName

I get a method or data member not found on the objws.protection line.

I am referencing the Excel 8.0 object library.

Can anyone help?? I'd really appreciate it. I'm new to the Excel object model!

Thanks!
Jeanine Scott
In Excel, to protect a specific range of cells, you first need to "lock" the cells and then protect the worksheet.

So something like this should work in your case:


objWS.Range("$A$2:$D$65536").Locked = True
objWS.Protect PassWord:="", DrawingObjects:=True, Contents:=True, Scenarios:=True


This code would be instead of your two protect lines.

HTH
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Mar 7th, 2002, 09:53 AM   #3
jscott
New Member
 
Join Date: Mar 2002
Posts: 5
Default

Thanks, Mark. I did try this - however what appears to happen is that the entire worksheet becomes protected.

I only want a certain range protected which is why I went in the AllowEditRanges direction. When I take the protection off the locked cells are editble.

What am I doing wrong?
jscott is offline   Reply With Quote
Old Mar 7th, 2002, 10:05 AM   #4
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Quote:
On 2002-03-07 09:03, Mark O'Brien wrote:
Yeah, my bad. MS locks all of the cells be default.

Try this before locking the range:


objWS.UsedRange.Locked = False


This will unlock any cells that have data in them. If you're not constantly adding data then you can do this each time. If you are then I would suggest something like this:



objWS.Range("A1:IV65536").Locked = False



To unlock all of the cells on the sheet the first time you run.

I'm not sure of the difference in runtime for these two pieces of code, but I would stick to the first one if I had a choice.


Also, the only way to protect individual cells is to lock them and then protect the sheet. So if the sheet isn't protected, the locked cells aren't protected. As far as I know, this is a limitation in Excel.

In your code, you might just have to protect the sheet again at the end.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Mar 7th, 2002, 10:16 AM   #5
Guest
 
Posts: n/a
Default

hmmm...
I did get this to work exactly the way I wanted it to in Excel by utilizing the "Allow Users to Edit Ranges" option on the Protection menu, then protecting the sheet. It protects my range while at the same time allowing the other cells to be edited.

Can you not do this via code?
  Reply With Quote
Old Mar 7th, 2002, 10:23 AM   #6
jscott
New Member
 
Join Date: Mar 2002
Posts: 5
Default

Sorry! forgot to put my username on the post...not trying to be anonymous


Quote:
On 2002-03-07 09:16, Anonymous wrote:
hmmm...
I did get this to work exactly the way I wanted it to in Excel by utilizing the "Allow Users to Edit Ranges" option on the Protection menu, then protecting the sheet. It protects my range while at the same time allowing the other cells to be edited.

Can you not do this via code?
jscott is offline   Reply With Quote
Old Mar 7th, 2002, 10:33 AM   #7
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Are you using Excel XP?

I've never used that and this could be a new feature in it. If it is, then it's long overdue.

To find out what the VBA is, just use the macro recorder to record your actions and look at the code generated.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Mar 7th, 2002, 10:39 AM   #8
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Yup,

you're using 2002.

Here's a link that'll be useful to you, I think it answers your question:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl10/html/xlproAllowEditRanges.asp>

Actually, no it doesn't solve your problem. What would solve your problem is going back to your original code then removing the unprotect line from your code. If you unprotect your worksheet, then all users can edit the sheet as they see fit. So the error generated is basically telling you that this line


objWS.Protection.AllowEditRanges.Add Title:="Classified", Range:=Range("$A$2:$D$65536"), Password:=""



is nonsense, because there's no protection on the sheet once it's unprotected, hence the method fails.

I hope this solves the problem. I really, really do.

_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-03-07 09:40 ]

[ This Message was edited by: Mark O'Brien on 2002-03-07 09:47 ]
Mark O'Brien is offline   Reply With Quote
Old Mar 7th, 2002, 10:45 AM   #9
jscott
New Member
 
Join Date: Mar 2002
Posts: 5
Default

Ahh! that explains it. I have to use the Excel 8.0 object library since my clients have Excel97. However I have Excel 2002 on my machine - so of course the 8.0 library won't recognize the 2002 properties. Well, that sucks - to put it bluntly.

Guess it's just going to have to be "one of those things"

Thanks for your help!
jscott is offline   Reply With Quote
Old Mar 7th, 2002, 10:54 AM   #10
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Quote:
On 2002-03-07 09:45, jscott wrote:
Ahh! that explains it. I have to use the Excel 8.0 object library since my clients have Excel97. However I have Excel 2002 on my machine - so of course the 8.0 library won't recognize the 2002 properties. Well, that sucks - to put it bluntly.

Guess it's just going to have to be "one of those things"

Thanks for your help!
Tell your clients to spend a couple of hundred (bucks, quid, yen) whatever on XL2002 if they really want it. I'm guessing this is a small percentage of the cost of any work they're getting you to do.

I've done this before going from XL97 and XL2000 when I hit problems with new features.

Although, they sound pretty cheap if they're still on 97.
_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-03-07 09:55 ]
Mark O'Brien is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 02:29 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes