Worksheet-level macros in Excel - 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.

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.

How can I list all worksheet tab names in another workbook in Excel?

I am working with 40+ workbooks and need a list of every named tab in each one, for a cataloguing project. The only macros and instructions I found require creating a formula or macro for each workbook.
I have one file designated as the macro file. In it are two functions, one to sort tabs by color and one to sort them alphabetically. Both work on whatever workbook I have active, as long as the macro workbook is open in the background.
How can I list all the sheet names in the active workbook, using code from the macro workbook?
I don't mind running the command once for each workbook, but I don't want to create a macro in every file.
You can use Workbook.Sheets to access the sheets of a workbook.
Likewise, you can use Workbooks to access the open workbooks.
You can create a nested pair of For loops to iterate through all workbooks, and then all worksheets.

Excel VBA - Copy module from a protected VBA project into a new workbook

My problem with Excel's VBA code protection:
In my current project, I wrote some VBA code (in WB1) that shall produce a new workbook (WB2). In this new workbook, a button (B2) is created. This button shall execute some code, when pressed. The macro that shall be executed is copied from the original workbook (WB1). This all works fine, when the VBA code in the original workbook (WB1) is not protected.
However, when I add protection to the VBA code in the original workbook, the module that contains the macro is not copied into the new workbook (WB2). And thus the button (B2) has no functionality.
My question: Has anyone of you run into the same problem and is there an other solution than removing the protection from the original workbook's VBA code?

Protecting/Un-protecting worksheet in shared workbook with VBA

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.

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