Excel protect and unprotect workbook VBA - vba

I have a workbook that contains several (lookup) tabs that I don't want any users to be able to unhide. However, there is one tab that contains admin info that I would like admin users to be able to see. I have a button and macro that prompts for an admin password before unhiding this tab, but if I protect the workbook structure (so that hidden tabs are not accessible for other users) the vba fails. Is there no way to protect/unprotect the workbook from within the admin macro? I have tried various combinations of ActiveWorkbook.Unprotect, ActiveWorkbook.Protect and ActiveWorkbook.ProtectStructure to no avail - both within the admin macro and WorkbookOpen. The error msg I most commonly encounter is "Can't assign to read-only property" even after having ActiveWorkbook.Unprotect at the top of my code. Does anyone know how to work around this? I don't really want to have to leave all tabs open to being unhidden with a second password on the Admin worksheet itself - clumsy!

Instead of protecting the whole workbook, set the Visible property of the sheets you want to hide to xlSheetVeryHidden - you can do this manually in the properties window of the VBA editor. Now those sheets won't show up in the Unhide… dialog. Your macro could swap the visibility of your admin-only sheet from xlSheetVeryHidden to xlSheetVisible and back.
By itself this doesn't protect your workbook from a user who knows how to access the VBA editor and properties, but if you need to do that there seem to be a lot of answers to a quick web search - it may depend on your version of Excel.

Related

Hide workbook/worksheets Excel VBA

I have a simple Microsoft Excel spreadsheet with a few tabs. On opening the workbook I have a USER LOGIN form that loads up and asks for login and password. What I'm trying to do is somehow not let the user see the sheets in the workbook until they've successfully logged in. The problem I'm seeing is that one of my sheets has a macro in it, so what I do
sheet1.visible=xlhidden
it gives me a debug error. However, even if I skipped that sheet and hid all the other ones - and on proper login I tried to make them Visible=xlVisible, they still did not become visible. How would I go about something like that?
Is there some kind of a way to be able to hide the workbook possibly and then make it visible after successful login?
xlhidden is not an acceptable value for Worksheet().Visible.
Sheet1.Visible = xlSheetVisible makes the Worksheet visible
Sheet1.Visible = xlSheetHidden: hides the Worksheet but allows users to unhide it
Sheet1.Visible = xlSheetVeryHidden: hides the Worksheet and prevents users from unhiding it

How secure is this VBA password protecting script?

I'm building a worksheet on microsoft excel and it will deal with sensible business data. The worksheet will be placed on a USB drive and have to move from place to place constantly. Therefore I've created a feature in excel VBA to enable/disable password protection. Here is how it works.
A page named settings is xlVeryHidden containing the user's password in a cell, another cell in the page contains the word yes or no. Depending on weather password protection is activated or not. When the user first opens the Workbook all sheets are xlVeryHidden except one containing a button to continue, they click on the button which runs a macro to check weather in the settings sheet the word is yes or no. Depending on so, they are prompted with a login or all the sheets are unhidden.
The code for the login is the following:
If PasswordTextbox.Text = ThisWorkbook.Sheets("Settings").Range("J5").Value Then
And after that all sheets are unhidden. Else it will give an error Msg Box.
Also the VBA code editor will be protected with excels default system.
What I'm asking here: Is my system fairly secure? For a regular computer user would this be hard to crack?
Thanks in advance :)
I'll add that it is pretty easy to unprotect your macro to get access to your code. If possible, you should think about implementing a protected database to store your sensitive data and populate your excel file from there, after proper authentication from the user.

How to edit, hide and protect a single sheet whilst also keeping other sheets hidden and protected

Thanks for taking a look at this question, I can't seem to find a solution that fits my needs.
I have a spreadsheet that has many individual sheets (sheet1,sheet2,sheet3...sheetx).
Sheet1 must always be visible.
Sheets 3,4,5....x must be hidden and protected such that they cannot be unhidden without unprotecting the workbook.
Sheet 2 must be normally hidden and protected as above but with the option of editing it after entering a password. The process of hiding and unhiding this sheet must not allow the user to see or unhide sheets 3,4,5....x
i.e the user must be able to hide,unhide and edit sheet 2 by use of a password but without ever allowing the user to see sheets 3,4,5,x etc.
Thanks again for having a look at this.
Charlie
You can try hiding sheet 2 (right click --> hide) and then password protecting the workbook.
But before that go to VBA and paste and run this:
Sub HideSheets
Worksheets("sheet3namehere").Visible =xlVeryHidden
Worksheets("sheet4namehere").Visible =xlVeryHidden
Worksheets("sheet5namehere").Visible =xlVeryHidden
End Sub
Doing this will remove the Unhide option for the above sheets from the user interface.
To unhide the sheets you will have to repeat the above but change xlVeryHidden to True.
Lastly you can password protect your vba project: Tools --> VBA Project Properties--> Protection. You can use a different password from the one that you have used for the workbook.
This way the needed sheets can be hidden without the Unhide option from the menu. While sheet2 can be still unhidden in the normal way if the user has the password to unprotect the workbook.

Excel won't show the workbooks worksheets

I have an Excel Workbook containing three worksheets. However all sheets seem to be hidden dispite that their visibility is set to xlSheetVisible (Found by looking at the worksheet properties in the VBA editor).
This is what I have tried so far to solve the problem.
Searched Google for help, but all similar problems have been solved by changing the Visibility from xlSheetHidden to xlSheetVisible. But in my case that setting is already set to xlVisible.
By using VBA I have tried to activate a sheet without any result.
By reading this you might have guessed that I am able to see and access the worksheets from the VBA editor.
Despite that looking at the normal Excel userinterface, all buttons are disabled and clicking at the office/file button in the topleft corner doesn't allow me to save the workbook. However if I make any changes to the VBA itself and tries to close the workbook then Excel asks whether or not I would like to save the workbook.
Everything you describe leads me to believe you're hiding the workbook window, not the worksheets. When a workbook window is hidden, all the sheets become hidden and much of the functionality from the toolbar greys out.
In the Visual Basic Editor, you'd also be able to see all of the worksheets and their visibility would be xlVisible, yet they cannot be seen on the screen. This is because they ARE visible, but the window which displays them is not.
Please try clicking the View tab in the toolbar, and then under the Window pane, click Unhide.
If this doesn't work, then try toggling visibility of the workbook.
It sounds like this may really be your problem...
http://vbadud.blogspot.co.uk/2010/08/hide-sheet-tabs-using-vba-hide-excel.html

excel vba - only enable cell edits through code

I have an excel tool that gathers information from a user based on their login information. The information is stored in cells on one of the worksheets. If I wanted to lock these cells so they can't be updated manually, how can I go about doing that? If someone else logs in, obviously these cells would change. I hope to do it through VBA.
I only want to lock 6 cells... everything else should be editable...
Thanks
You can protect the worksheet with a password like this:
Private Sub Workbook_Open()
Sheets("sheetName").Protect Password:="YourPassword", UserInterfaceOnly:=True
End Sub
That will prevent users from manually making changes to the worksheet without entering the password. Your VBA code will still be able to make changes because you've set UserInterfaceOnly to True.
Note that users could easily view this password by navigating to the code through the Visual Basic editor. You can password protect the code as well, though: just right-click on the module, click on VBAProject Properties and go to the Protection tab.
See this page for more information: Excel VBA: Macro Code To Run Macros On Protected Worksheets & Sheets.
If these cells are the only ones on the sheet you want to protect, then just change the cell properties of the remaining cells by changing Locked property to false and leave the cells in question as Locked then protect the sheet using UserInterfaceOnly set to true (but realize that this doesn't work for all possible macro changes, so I usually avoid it.)
There are other methods that could work, but I think this is the best solution for you. If not, please add a comment to let me know.