Protecting/Un-protecting worksheet in shared workbook with VBA - excel

I am wanting to put a notes section into a workbook, where notes can be added about a particular row in a worksheet. There will be lots of rows.
I want these notes to be uneditable once they have been written, so they can't be tampered with.
Previously I have done this by unprotecting, then protecting the worksheet to enter the note which is captured in VBA. However this workbook needs to be shared, which means you can't unprotect or protect a workbook either in VBA or manually.
Excel really isn't the right platform for this, as it's basically a small CRM, but unfortunately for the moment it's the only thing available and that can't change for now, so need to try and find a way to make it work, any idea's?
I have tried protecting the workbook with UserInterfaceOnly:= True, and that works great until the workbook is closed and re-opened then it fails and then can't unprotect or protect the workbook.

Related

Unprotect or remove Bloomberg BbgResearchPubStorageWorksheet in Excel workbook

Excel workbooks that have used the Bloomberg Add-In contain a hidden protected sheet called BbgResearchPubStorageWorksheet. The fact that sheet is protected prevents certain functions from being performed on the workbook – e.g., deleting custom styles. How can that worksheet be unprotected or removed from the workbook?
Unprotecting from the File >> Info menu claims to require a password, as shown in this screenshot:
(So far nobody in Bloomberg support has admitted that they have anything to do with this worksheet, so they can't provide the password to unprotect it.)
BbgResearchPubStorageWorksheet also appears in the Excel Objects list, but cannot be removed from the VBA editor:
How can we unprotect or remove this BbgResearchPubStorageWorksheet?
(One kludge would be to copy everything else in the workbook into a new workbook, but for a complicated workbook is becomes very tricky to preserve all references, names, and VB code in that process.)
First expose the sheet by going to its properties in the VBA editor and changing its Visible property from xlSheetVeryHidden to xlSheetVisible:
Now the worksheet will be visible and it can be deleted, even without knowing the password that was applied to protect it.

Worksheet-level macros in Excel

I have some macros stored in worksheet module in an Excel file.
I protect the whole VBA Project with a password so the code cannot be seen.
But if that worksheet is moved or copied to another workbook, the code becomes visible without any passwords required.
Is there a solution for this?
I am deliberately putting my macros into a worksheet module because I want users to copy this worksheet into their files and work with the macros. But I want to keep them locked for viewing.

Excel VBA: Lock cells without protecting the workbook

I have created an add-in that sends and retrieves data from a database in order for this data to be used by our analysts.
To prevent changes made to existing data points I want to lock the cells containing this data. Initially, I did this by locking the range of the data and protecting the workbook, since otherwise the locking does not work. However, protecting the workbook also removes/limits a lot of functionality for the end-user, such as creating graphs, the auto fill function, changing the format etc. Since these and other functionalities are needed for the end-user, I cannot protect the workbook. Still, I want to lock the cell containing the data points.
So my question is, is it possible to lock the cells in a dynamic range (I have macros detecting the correct end column and end row of the data points) without protecting the workbook? If so, how? If not, would it be possible to detect changes in the dynamic range and show a messagebox that changes are not allowed in this specific cell/range and revert back to the old value of the cell? If so, how?
I have seen a few posts asking a similar question, but these were either not answered or the answer was not satisfying for my case (e.g. a macro implemented in the VBA project of the workbook instead of the VBA project of the add-in).
Thanks in advance for your answer(s)!
Kind regards,
Robbert
Use
ActiveSheet.Cells.Locked=False
Then Lock your range which you don't want to be edited using:
Range("A1:A4").Cells.Locked=True
Then protect the sheet
ActiveSheet.Protect Contents:=True, DrawingObjects:=False
This will only protect the contents that are Locked and still allow you to change values in the rest of the sheet and insert/delete charts. You may have to change extra settings with Protect. More details can be found here
You don't have to protect the Workbook, only the Worksheet.
To protect/unprotect a sheet via VBA, use Worksheet-methods protect resp. unprotect (you can apply a password).
To prevent a range to be modified, you have to set it's locked-property to True and protect the sheet (all cells have the propery locked set by default).
Be aware that, if a Range is locked and the worksheet is protected, you cannot modifiy the Range via VBA. If you want to do so, use the unprotect method at the top of the code (but don't forget to protect the sheet again when you're done). As an alternative, you can call protect with parameter UserInterfaceOnly:=True, but unfortunately Excel doesn't save this property. So if you save an Excel file where a worksheet is protected with UserInterfaceOnly:=True and reopen it, the sheet is protected also for VBA.

Copy from unprotected workbook to protected workbook - Excel 2013

I received a protected excel file which only allows me to add values or select from a drop down list. The file has lots of different tabs, all in different format and questions.
I then unprotected the workbook to make my life easier (i.e. copy, paste, make notes etc)
using the code found here:
http://uknowit.uwgb.edu/page.php?id=28850
Now i am looking for a way to transfer all the values from the unprotected file back to the original file they sent me as I cannot submit an unprotected file. It is too many questions to do manually.
What is the best way to do this in excel 2013/VBA?
Thank you
It looks like you're wanting to do this on a sheet that previously had a password you didn't have access to. Also, it seems that you can't ask the worksheet creator to simply remove the password.
If both these are true, the best solution would be to save the modified sheet with the same name and send that sheet. From what I've seen, VBA and developer tools don't include a way to copy values to locked regions in a password-protected sheet, without unlocking that sheet:
ActiveSheet.Unprotect 'method to unlock current sheet, password may be required
ActiveSheet.Protect 'same to lock again
But, once a sheet is unlocked, you could loop through all cells in the region you want to copy from, and copy those values to the new sheet / workbook.
Please let us know if there's something else, or if something needs to be explained in more detail.

Adding Module through code

I have a master workbook containing a macro which opens another workbook containing a demand forecast. The workbook opened through the macro is downloaded from a customer portal and is all new every day without possibility of editing it beforehand.
The macro then loops through the information and creates new readable and more intuitive worksheets. However, on a few of these worksheets I would like to add some event-driven code to give tooltips when mousing over or selecting cells.
Is there any possibility (without installing add-ons from vanilla Excel 2010) to add code to worksheet objects created during a macro?
The layout of the processed workbook is more or less static, so I was wondering if I should create a template file and then copy the input into it. That would allow me to have coded the events before data is added. Is this the best possibility?
As Dan pointed out, you could use Application.VBE.ActiveVBProject to programmatically add code modules to a workbook. But doing this requires more lenient Macro security settings (which are set to untrusted, by default) and this is not recommended.
When I have to do something similar, I use three workbooks:
The data containing workbook
This book has no macro functionality
A template workbook containing the necessary macros
A macro enabled workbook to facilitate the transition.
Use workbook #3 to open workbook #1 and copy its data into workbook #2. Save a copy of workbook #2 and close it. Repeat this process as necessary.
It isn't the prettiest solution, but it keeps your code modular.

Resources