Run macro in shared workbook and protect it - excel

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.

Related

Open Excel worksheet - run time error with other macros

I made a workbook with several sheets for data entry, and I want the index/main sheet to display upon opening every time.
Everything on the internets says to use something like below, but I get a runtime error.
I've made sure the worksheet is named "Attendance Main" and the spelling is correct.
Most troubleshooting issues I've seen are about activating sheets for copying/pasting and other actions. I just want the main sheet to display each time the workbook is opened.
When I create a new workbook, with 2 basic sheets, the macro works. So I am wondering if other macros are interfering with it. However, none of my macros are in "ThisWorkbook", most are in their own modules.
Any help appreciated, and if you need any info from me to narrow down the issue I will try to provide what I can.
Private Sub Workbook_Open()
Worksheets("Attendance Main").Activate
End Sub
Run-time error '1004'
Activate method of Worksheet class failed
For me a simple select code inside the open event of Workbook works fine.
Private Sub Workbook_Open()
Sheets("ShowThisFirst").Select
End Sub

How to protect a specific workbook only in Excel VBA

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

Excel: problem with deleting .Connections in a protected workbook

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.

VBA Shared workbook and Unshared workbook

I tried to find the code to share workbook and unshared it with visual basic but I didn't find it, anyone know if its possible?
Another thing is shared workbooks when saved, update the workbook to all users...
the question is if I save it with visual basic code the workbook will update to another users?
I am coding an button that when clicked it (share the workbook > fill the cells > save and unshared it).
I certainly agree with pnuts and the link he provided: Shared Workbooks are horrible.
To respond to the question though, if you record a macro in Excel you will see code like the following when you share a workbook.
Sub Macro1()
Workbooks.Add
With ActiveWorkbook
.KeepChangeHistory = True
.ChangeHistoryDuration = 30
End With
ActiveWorkbook.SaveAs Filename:= _
"F:\Documents and Settings\student\My Documents\Book1.xlsx", FileFormat:= _
xlOpenXMLWorkbook, AccessMode:=xlShared
ActiveWorkbook.ExclusiveAccess
End Sub
(If you don't already know how to record a macro in Excel then I recommend that you take the time to find out - it is extremely useful, particularly when you are just starting with VBA.)
If you copy this code into the VB Editor and click on certain words (SaveAs in particular) and press F1 you will get into the Help system.
From this recorded macro I surmise that removing Shared from a workbook is just a case of using SaveAs with an AccessMode other than xlShared (or omitted). After all, this is the dialog/option that appears when we manually share or un-share a workbook.
But, to emphasize, I am not advocating the use of Shared Workbooks.

VBA Excel- How to do Unprotect and protect through vba

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.

Resources