Lock or Freeze VBA Code to Prevent Editing - vba

Is there any way I can lock or freeze the VBE editor to prevent others from accidentally editing the VBA code?
I don't want to lock down the whole workbook where people can run macros but can't see the code.
I want to let others see the code but don't want them accidentally deleting something and break the program (something even I am prone to doing to my code).

Just go to Visual Basic, right click your module, select VBA Project Properties..., under protection tab, you can lock your VBA code with password.
Hope this help. ^^

If you want the code to be visible and executable, then you can't stop people from making changes to it.
You can however, stop them from saving those changes, by opening the workbook as read-only.
You can mark a workbook as read-only in the File Properties from within File Explorer. Or, if your workbook is opened via Automation, then you can specify read-only in the open method.

Related

Only save the code

I'm currently learning Excel VBA and since CTRL+Z doesn't undo anything, I'm trying to find a way to undo what my code does when I have to. On my office computer, I can click on the Save button from VBE and it saves my code, but nit the workbook.
On my personal computer (Office 365), when I do the exact same thing, it saves everything, code and workbook so I cannot undo what my code did by not saving it.
Can you help me to understand why? And do you have any idea I can undo what my code did?
Thank you
You have two options:
1) Export code modules to file - either manually by right-clicking on them, or via code (check out VBA Extensibility), or;
2) Do what I do, which is the easy option of just copying chunks of code as text into Notepad.
There doesn’t exist what you’re really looking for: an integral method of “saving” the VBProject but ignoring the workbook - as the VBProject is part of the workbook when it comes to file structure.

Way to capture double click to open file?

Is there a way to have an Excel macro to check what file I double clicked on to open.
When I open that file, the Add-Ins installed load first, then the file I clicked on loads. How can I write code inside one of my Add-Ins to check what the filename is that I am trying to open?
I tackled this by using 2 instances of Workbook_Open inside an excel Addin.
When a file is loaded, the addin starts up, and checks to see if there are any active workbooks. If there is none, then we wait a little bit and check again, looped as many times as you deem necessary, or until there is a valid workbook open. This makes sure your computer has time to load up the excel file. Once there is an active workbook, we process it to make sure it is not the workbook of the addin. If it is, then we exit our code. If it isn't then we Do Stuff.
I solved 2 of my questions on this site with the same code that can be found HERE
Note: I left out the parts where I Do Stuff... because really it's there to do anything you want with it. The code in the link just does what the previous paragraph describes.

Excel not responding when I try to open VBE

I need help, Excel is not responding. I have written a lot of code in VBA and right now I can't even open VBE. I can open the workbook okay, but every time I make any change to one of the cells (I have a lot of code that is triggered by the worksheet_change event based on the cell that was changed) it will not respond. I'm 80% sure it is something wrong with my code, but I can't open the VBE to diagnose because that will also cause Excel to not respond, forcing me to close the application.
I have tried opening Excel in user-initiated safe mode, but this doesn't fix anything.
Any ideas??
Change your Security Settings to prevent macro execution. You can then open file and examine the macros without fear they will execute.
EDIT#1:
Close all files and programs. Then, restore your computer to an earlier time period. You can find out how to do this in 'Control Panel'. This will fix it.

Only I am able to save a macro-enabled workbook?

All, I have created a workbook that has some macros in it to import data. The idea is that the file is a master file, and every time you import data with it, it is supposed to place that data on the end of the existing data, and then you save it and move on.
The problem is, I am the only user that can save the workbook. Now, two of the sheets in my workbook I have protected, so that they cannot be edited. I have done this so that nothing can accidentally be removed (buttons, instructions, notes, etc). My users have agreed that this protection is a good thing.
But what I think is happening is our network is making anyone who didn't author this file open it as Read Only, and then they cannot save to it. I first thought maybe if added a save macro (and command button) that it would fix it. No dice.
Next, I had the workbook unprotect, and then re-protect itself when the user clicks the save button. Nope, still opening as Read Only.
I then put code in the Workbook_Open() Sub that changed it from Read Only to Read Write. This caused a box to popup when opening the sheet that said the file was in use by "Another User," and it was locked for editing.
The last thing I tried was adding the other users as Authors to the workbook. And it STILL opens as a Read Only file.
I think this has to do with the network settings here in our office (well, corporate-wide, but anyway). These are policies that cannot be changed. Can anyone help me find a work-around that allows my sheets to be locked for editing, but allows my users to save to my workbook?
You can see here that I have added three other users as Authors of the file (This is the information page of the file as opened by a user.):
So it turns out that the issue was not related at all to the workbook...
When attempting to Save As in the folder where the workbook was located, we found out that the user I was using for the tests simply did not have write permissions to the folder in question. I had write permissions to it based on a previous assignment.
So much frustration over something so simple.

Standalone code for Excel

Can VBA code be written to perform actions on any Excel file?
When I create a project in Visual Studio, it asks for an Excel file to be linked to it. All the code that I write is in ThisWorkbook.vb and hence acts only on the Excel file linked to the project.
Ideally, I want a script that:
When the user double-clicks, he/she should be allowed to select an Excel file of choice for the actions to be performed on that file.
If the above is not possible, I'd at least like to invoke the VBA script from within an Excel file.
Basically, the VBA code should be independent of any Excel file.
I looked at VBScript, but the syntax for it seems to differ slightly.
You've mentioned Visual Studio, VBA, and VBScript. The solution I'm outlining works directly with VBA rather than Visual Studio or VBScript. (However, you might adapt Visual Studio (C# or VB) along the lines of what I'm outlining below.) Hope it's helpful, so here goes:
Here's what I do, and, it ultimately results in an .xlam Excel AddIn as #chris above has commented.
Overview
I start with ordinary .xslx workbook, to which I add a VBA project, making it an .xlsm. Then create some VBA Subs (macros). Then create some Excel QAT (Quick Access Toolbar) buttons for the workbook, which are bound to (i.e. they invoke) the VBA subs/macros. Then convert the workbook (with VBA in it) to an .xlam, which is an Excel AddIn. When you're all done, the buttons are accessible from any workbook (and the VBA code has access to any user workbooks as well as those originally in your .xlsm). Further, the workbook associated with the .xlam is invisible. So it just looks like you've added some buttons to the QAT that appear on all users .xlsx windows. The .xlam is pretty easy for users to install (though I provide a buttons to uninstall/reinstall/check version). You can upgrade an .xlam independently of users' workbooks; users' workbooks can thus be data only (.xlsx, no VBA).
Details
Write some Excel Subs you want to use later
You need to be aware that the buttons can only invoke macros (VBA Subs) without parameters, so the macros will have to check things like ActiveSheet and ActiveWorkbook and Selection to figure on what sheet the button was pressed, hence what user data to really operate on. (If you need to refer to your workbook with the VBA code in it, use “ThisWorkbook”). You should be aware that there can be naming conflicts, so try to name the parameterless subs with rather long names, such as MySomewhatUniqueProjectName_button1, etc…
Add Buttons to your .xlsm
Using Excel 2010 (I think this works with 2007 or later), I put workbook-specific buttons on the QAT part of the ribbon, which connect to macros (VBA subs) in the VBA code.
To do this, you from the Quick Access Toolbar customization drop down (the tiny down arrow at the very top row of the Excel window, the last icon from left to right) choose "More Commands…". When the “Customize Quick Access Toolbar” dialog box comes up, from the (2nd) "Customize Quick Access Toolbar:" heading (top to the right), choose "For XYZ.xlsm" from the dropdown instead of the "For all documents (default)". And from under "Choose Commands From:", use "Macros" (instead of “Popular Commands”) from the dropdown. Once you have those two things selected, you can move VBA subs from the left box to the right box using “Add >>”. Each so moved will become buttons visible in your QAT. As you’re doing this you can also edit the icon and text for the buttons, add a separator as needed (I always end with a separator in case other .xlam’s use the QAT). (Now is a good time to save this .xlsm.)
Convert the .xlsm into a .xlam
Then I convert the .xlsm to an Excel add-in, by merely saving it as an .xlam file. This will end up (1) hiding the workbook associated with the code you have (though it is still accessible to itself.). Further, now, (2) the (invisible, as now it's an .xlam) workbook will load whenever Excel is loaded. (To keep this fast for when users use Excel but don’t run my VBA code, I don't run any code when the .xlam is loaded, I only run code when a button is pushed.)
You can manage the AddIn using Excel’s AddIn manager. To update the AddIn, you have to use some trickery. While you can copy over it when Excel is not running, on the other hand, you cannot directly uninstall the AddIn, you can only disable it from Excel. But once disabled, you can delete the .xlam, and relaunch Excel, go to the AddIn manager to try to work with the (now gone) AddIn and you’ll get Excel saying it can’t find it, so do you want to delete it. Say yes, and it will be uninstalled.
FYI, Notes
I keep the .xlsm to edit later, but you can actually debug and edit the .xlam and later convert it back to an .xlsm with a minor bit of trickery: find its "ThisWorkbook" entry in VBA, and then the "IsAddIn" property, set to false, its workbook will suddenly appear and you can save as .xlsm, or edit its workbook and set the property back to true to resave as .xlam directly.)
Answer 1
You can do that in VB.Net too. Just make a regular project. (comment by #SLaks)
This worked for me very well and was exactly what I was looking for.
Answer 2
The very descriptive answer posted by #ErikEidt
I haven't tried this, but it seems like a good and alternative way of getting macros to work.