I have a workbook in which i have several sheets according to requirement. In this workbook i have used macros and to lock some particular cells and columns i have used unprotect and protect options. Everything works fine on my machine but when i make it as SharedWorkBook it is giving me errors particularly the ThisWorkBook.Sheets("PSE").Unprotect and ThisWorkBook.Sheets("PSE").Protect statements. So in place of them i used ThisWorkBook.Sheets("PSE").UnprotectSharing and ThisWorkBook.Sheets("PSE").ProtectSharing . Even now also i am getting errors with those lines.
Sub SheetHider()
ThisWorkbook.Sheets("SheetA").UnprotectSharing
Cuser = ThisWorkbook.Sheets("SheetA").Range("A2").Value
and the error is Runtime error '438' Object doesn't support this property or method.
My Requirement: 1)I have to use Macros, Locking property so that i have to use Unprotect and Protect statements and these things should work when i make it as SharedWorkBook.
I have been searching many sites for this for a week. Atleast give me some alternatives to do this.
Any help will be appreciated greatly. Thanks
UnprotectSharing and ProtectSharing both belong to the Workbook object. So you can't do what you are trying to do.
You can do ThisWorkbook.UnprotectSharing and ThisWorkbook.ProtectSharing but that is only design to protect the Shared Workbook setting so that people can't remove it without a password.
Secondly you can't unprotect or protect sheets when a workbook is shared. You would have to unshare the workbook, unprotect the sheet and then share the workbook again. Which isn't very practicable.
You must protect sheets before protecting a shared workbook, unprotecting goes in the reverse order.
Related
I would like to lock the integrity of a workbook so that users can't delete or move sheets. However I would still like to be able to hide and unhide sheets using VBA. Is there any way to achieve this?
You may unprotect the workbook, do your stuff and protect the workbook back:
ThisWorkbook.Unprotect Password:="your_password"
'[... do your stuff...]
ThisWorkbook.Protect Password:="your_password", Structure:=True
Use this code when you set the protection.
Sheets("Sheet1").Protect Password:="myPassword", UserInterfaceOnly:=True
The effect is that the user can't change anything that you haven't allowed (there might be more to the above code than I show here) while the UserInterfaceOnly property allows your code to act as if no protection existed.
What is the best way of protecting a specific Excel workbook?
I have an inherited script that includes the following common lines at the end:
ActiveSheet.Protect "my-password"
ActiveWorkbook.Protect "my-password"
However, I've noticed that as the script can take a few minutes to run users often switch to a new unrelated workbook whilst it solves - whatever else they are working on. The password protection is then inherited by the unrelated workbook upon the completion of the macro - since whatever other Excel file the user is working within is now "Active" (presumably? this is my reading of the problem).
The above script is in a workbook that can be renamed to whatever the user chooses, and be saved in any number of directories. How can I ensure that only the original excel file is locked/unlocked by the Macro, when other workbooks are in use?
I am sure there are many ways to do this, but which is the most foolproof method?
NOTE: using office 365
Thanks Dean's answers in the comments:
Early in the code (and in Worksheet_Change if appropriate) enter the following to define your sheet as an object (named default_ws in my case):
Set default_ws = ActiveSheet
When you are ready to lock your sheet or workbook you can then use:
default_ws.Protect "password-here" 'protect your sheet
ThisWorkbook.Protect "password-here" 'protect your workbook
Also note:
You could also define your workbook as an object as follows if desired:
Set default_wb = ActiveWorkbook
While trying to remove external connections from my protected Workbook (which imports data from a csv file) with VBA, I run into Runtime error '5'. Strangely, it works if I unprotect the Workbook.
I found this thread: Protect Excel Worksheet For Read Only But Enable External Data Refresh, but the proposed solution of unprotecting the workbook while the scripts are running is out of the question.
Here is the code that I use to remove the external data connections:
Sub RemoveExternalDataConnections()
Dim i As Long
For i = ActiveWorkbook.Connections.Count To 1 Step -1
ActiveWorkbook.Connections.Item(i).Delete
Next
End Sub
All in one, I would like the external data connections be removed even if the workbook is protected.
For anyone else - it seems that unprotecting the workbook is the easiest way. Setting up a password on a document/VBA project is not particularly safe anyway and it will not block most tech savvy users from accessing it.
I want to run a macro in a shared workbook. My macro as below.
Sheets("Sheet1").Unprotect
Range("A1").Select
ActiveCell.FormulaR1C1 = "ss"
Sheets("Sheet1").Protect
When I run the macro without sharing it works fine. When share it below error prompts:
Unprotect method of Worksheet class failed.
Can anyone help to run my macro in shared environment and keep the sheet in protection?
This is a limitation of Excel.
Sheets can't be protected or unprotected whilst shared. No way around it short of changing Excel itself. Afraid there's no other way.
You'll also notice that you can't manually unprotect the sheet whilst it is shared as well.
I am using VBScript to make lots of calculations on different sheets on the same workbook. Now one concern is if somehow someone deleted any of the sheet, the script would then lead to massive data loss. Is there any way to protect it from users?
(Make sure you read the edit at the end of this answer)
I have similar workbooks I have made that use sheets like databases, so I understand your concern of protecting the information. If you are protecting the Workbook, it will prevent your macro from performing certain changes to the sheets/book. However, if you only want to protect the sheet contents, you can use the setting UserInterfaceOnly, which will allow your macro to run freely while still protecting the sheets from the users. You can detect if the sheets are protected when opened, and if not, protect them.
Sub test()
dim mySheet as worksheet
dim myPass as string
myPass = "password"
set mySheet = Excel.ActiveSheet
if mySheet.ProtectContents = False then
mySheet.Protect Password:=myPass, UserInterfaceOnly:=True
end if
'Enter your code here
mySheet.Unprotect Password:=myPass
End Sub
Furthermore, if you want to give the user the ability to unprotect the sheets after being warned, then you can use Custom UI Editor to remove the protect/unprotect sheets group from the review tab and then replace it with a customized button. I've done this in the past and it actually helped a lot with maintaining the sheets later. I've included a couple links on Custom UI Editor below.
Custom UI Editor Download
Custom UI Editor Tutorial - Change the Ribbon in Excel 2007 or
2010
EDIT:
At first glance I thought this question pertained to VBA (my bad). I believe the above code can still apply, just place the code within the tags and set the workbook and worksheet variables properly (not the way they are set above). If you don't already know how to set/reference your workbook/worksheetsheet objects, this forum post shows how. Albeit I don't normally work with VBscript, but the forum looks correct. Good luck!
i nw this was a few years ago, but i ran into it when searching for the answer, hopefully someone fins this helpfull.
First add a password to your workbook that way other users will only be able to open as read only.
http://www.k2e.com/tech-update/tips/728-tips-adding-a-password-to-make-an-excel-workbook-read-only
then to make as part of the vbscript when opening the excel file(s)
Set oExcel = CreateObject("Excel.Application")
Set oWorkbook = oExcel.Workbooks.Open(file.path,,,,"password","password",True,,True,True)
full list of switches can be found here:
http://msdn.microsoft.com/en-us/library/office/ff194819%28v=office.15%29.aspx
hope that helps, took me hours to figure this out.