Capturing the password entered into an Excel sheet protection with VBA - vba

I have a worksheet with a table in, that I have protected via a password I have defined.
I also have some VBA code running under the Worksheet_SelectionChange event which checks if the current cell is within the table, if yes it unprotects the sheet and if no it reprotects the sheet with my predfined password.
I am aware though that when inside the table (hence unprotected) the user could enter a new password for the sheet protection which will cause an error to the running of the macro.
Does anybody know if it is possible and if so how to capture the password a user enters into the sheet, that I could store into a VBA variable?
My thinking is that I could then run something further to unprotect instantly with that password and then reprotect withmy predifined one?
Thanks

Related

commandbutton (active-x) changes pathname of assigned macro

Not sure if relevant for solution, but just so you see the workflow, file is set up as follows:
One sheet is called "Template".
User enters info on another sheet called "Enter Info".
Click on cmdbutton "open client" and the assigned macro copies the sheet "Template" and renames it using certain data (client name) from the "Enter Info" sheet.
This all works fine.
In the "Template" sheet (which gets copied) I have 2 command buttons and have assigned a macro. The macro is saved "in this worksheet".
Example: "Filename.xls!macro_do_something"
This has worked for 2 years now, but all of a sudden every single button in every single sheet (not just new copies) has been assigned to the macro:
"Filename comp.xls!macro_do_something"
When I click to execute the command I get the message that the file could not be found, which makes sense as it does not exist and never has.
I have never copied the file to a different location, I have never changed the file name. It worked fine and correct for 2 years.
Any ideas how this can happen?? Why would the file name just randomly get changed??
As you've been cloning sheets for two years, the workbook could have reached some maximum number of sheets. Some magical 32, 64, 512 etc. Perhaps reaching such a limit would produce an unexpected behaviour, like changing the file name in the buttons assignment.
One other thing you could check for is the presence of a special character in the sheet names the workbook assigns after the client names.

Error on protected sheet

I have macro enabled workbook. Both Workbook and sheets are password protected.
Basically, I have 2 sheets. First "crater" which has a drop down list and buttons.
Second, "Uno" which has buttons again( Its a hidden sheet)
Button 1 on crater page task is to just copy the "Uno" sheet and create a new sheet. All tasks are working fine.
But my problem is, when I create a child sheet and save and close the workbook.
Next time, As soon as I open workbook I get an "error '1004'. You cannot use the command on protected sheet. You must first unprotect sheet" .
I would like to know, Where can I use the code to unprotect sheet in this case?
I am confused, please help
Use the command Worksheet("Your_Sheet_Name").Unprotect if you have a password you need to add it as well Worksheet("Your_Sheet_Name").Unprotect(Password)
You can have it in the Workbook Open and it will unlock the worksheet. However, then all the users can modify this worksheet as well.

Disable any incoming external links to a Excel Workbook

I have two workbooks. Say test1.xlsx and test2.xlsx and have saved them in the same directory.
I write a formula to create an external link in cell A1 of sheet1 of test2.xlsx.
Example:
In the A1 cell of sheet1 of test2.xlsx I write =[test1.xlsx]Sheet1!A1 and press enter. It copies the corresponding cell content of test1.xlsx into test2.xlsx
I don't want this formula to be executed. I want that my workbook "test1.xlsx" shouldn't be accessed by any other external workbook i.e. no one can provide an external reference to test1.xlsx and if provided, it should return any error say "!#REF"
I am using Excel 2010.
It will be quite helpful for me to know exactly what are the events taking place in test1.xlsx
Not exactly the behavior requested, but you could do this:
Password-protect test1. The user will be prompted for the password when the formula is entered.
If the password is correctly entered, the formula(s) will execute, but the users will be prompted for the password to update links each time test2 is opened.

Is it possible to have a cell that has a formula and accepts entry at same time in excel?

Example:
A B
1 =vlookup(XX)
2
3
in cell A1 there is a Vlookup formula, Is it possible to enable user entry in this cell and override the formula then later restore the formula automatically when sheet is open again?
Even through VBA
Short, boring answer: nope.
A cell only ever has a keyed-in value, or a calculated formula. Can't have both.
Longer answer: maybe.
Shift everything 1 row down, and use row 1 to store your "original" formula - then hide that row (and pray the user isn't going to mess with it).
When the sheet is opened again sounds like you're confusing "workbook" and "worksheet" - you need to handle Workbook_Open if you want to run code when a workbook opens. Workbooks contain worksheets - it's the workbook that opens, not the sheets (sheets activate, but I doubt you would want to put that logic in there).
So, in the handler for Workbook_Open, write code that takes the formula in the hidden row and overwrites whatever is under it.
Another solution can be to hard-code the formula in the VBA code.
One possibility would be to store your Workbook as a template. Normally when a user opens the workbook by double-clicking, it will open whole new workbook based on the template, and they can modify it to their heart's content, save it, mail it to Grandma, etc.
The next person who comes along will double-click the template file and get the formula again, just as you designed it.
Short answer: Kind of, sort of
Long answer:
Save your workbook as a template. Every time someone will use it you'll see the orignal with formula, then if someone write over the formula, when using save your original will be kept intact.
What You need to do is:
press Alt + F11
select ThisWorkbook and paste this code:
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("A11").Value = "asdf"
End Sub
Every time the workbook is opened, this script will run.
Instead of "Sheet1" you can write the name of the sheet you want to apply the script.
Inside the Range, You can define the cells you want to modify, You can use even multiple cells. Check this for more information about this.
After Value You can write what You want to be written inside the cell. You can write "=vlookup(XX)" and it will work.

Sheet.Activate changes sheet but continues to edit data on previous sheet

I have a dialog box with a couple of buttons that launch macros to activate and change to different sheets.
The problem I am having is after I click the button, the macro activates the new sheet and I see it. But when I go to delete data, add data or try to delete a row "Nothing happens" the data on the screen is still there. If I go back to the previous sheet, the cells and rows that I had intended to delete were deleted in that sheet. It is very wierd and never seen anything like that. It appears that my macro code is note doing enough to actually change to the new sheet. I do not have this problem if I click a different sheet tab to change to it. Or if I click the dialog button to go to the new sheet and quickly do a ctrl-pgDown and Ctrl-PgUp to change from another tab and back that seems to fix the problem.
This is the code in my macro I am using to try to change to the desired tab.
Private Sub Report1Button_Click()
On Error GoTo Handler
Sheets("Report1").Activate
If StayOpenBox.Value = False Then
Unload MainMenu
End If
Exit Sub
Handler:
MsgBox "Sheet 'Report1' not found or renamed"
End Sub
Thanks for any help or suggestions
UPDATE:
Here is code that I use to call the dialog box. I have a shape on the other sheet that is assigned to this macro to open dialog box
Sub ShowMainMenu()
With UserForm1
.Show
End With
End Sub
Also there is no further code to make edits to the new sheet. My Button click simply switches to the other sheet and when I attempt to make edits manually, they are actually done on the previous sheet which is not the one I am currently looking at. So anything I do, Bold text, delete text, delete row, etc, is not done on the current sheet I am looking at, but when I return to the previous sheet the changes where made there. Im on Excel 2013, I have reproduced this problem in 2 separate files, but I will try on a different computer and older version of excel. Screenshot of my situation is below.
UPDATE 2:
I ran this xlsm file on a 2nd computer with Xls 2007 and was not having the problem. So I ran the macro on a 3rd computer that also has Excel 2013 and it is experiencing the same problem. So it is not computer specific and seems to be a problem in XLS 2013 but not in XLS 2007. I will try to find a computer with Excel 2010 to test as well, but something about this code is causing a problem in 2013 but not in older versions of excel.
When you run VBA code, it will default to using the ActiveSheet if you don't define the Sheet. When you have objects/methods that you want performed on a specific sheet, you should always specify! You can do that one of two ways:
Sheets("Report1").[Object].[Method]
'or
Sheets("Report1").[Method]
or you can pass the Sheet name to a variable and use that for shorter code
Dim Report1 As Worksheet
Set Report1 = Sheets("Report1")
Report1.[Object].[Method]
'or
Report1.[Method]
Try changing the sequence of lines in your code. I had a similar situation and it turned out that I inserted the "delete sheet" code in between the commands that were copying data from sheet1 to sheet2. When I put the deletion lines (commands) after I finished copying sheets everything started to work correctly. Many commands activate one sheet while performing and this immediately disactivates another sheet. So if you used "ActiveSheet" sommand somewhere it may be incorrectly understood - the command may be executed not on the sheet you meant.
Just use the full address for the range you are trying to manipulate, for example instead of:
Sheets("mySheet").Activate
Range("A1:B10").Cut
Sheets("myOtherSheet").Activate
Range("A1:B10").Paste
use:
Sheets("mySheet").Range("A1:A10").Cut Destination:=Sheets("myOtherSheet").Range("A1:B10")
I just had the same problem, also with Excel 2013. So even if the thread is over 9 month inactive, I want to share my solution in case somebody gets here through a Google search.
The solution was really simple. Call the userform with:
UserForm1.show vbModeless