excel created a password to unprotect a sheet without me telling it to - vba

I have a protected sheet with certain cells unlocked for editing. I have button click macros that run various processes that temporarily unprotect the sheet to allow the code to run, then protect it again when done. example:
sub macro1()
activesheet.unprotect
' code here
activesheet.protect allowsorting = true
activesheet.protect allowfilter:= true
end sub
for some reason when I run these macros now, it is asking for a password that I never put in there. the sheet should not be password protected. I ran a password breaker macro and it told me the password is "AAAAAAAABABF"
what would cause this, and how to I remove it from asking for a password?
can't seem to find any results in the forum with this issue.
thanks for your help

You have an error in the code, thus the password protection gets activated on this line activesheet.protect allowsorting = true. To avoid errors like this one, make sure that you always use Option Explicit on the top.
In general, this should be ok:
Sub Macro1()
ActiveSheet.Unprotect
'code
ActiveSheet.Protect AllowSorting:=True
ActiveSheet.Protect AllowFiltering:=True
End Sub
Concerning the AAAAAAAABABF, it is not the password you have set, but its hashed value is the same as the hashed value of your password.
If you want to see yourself, try just this code:
Sub TestMe
ActiveSheet.Unprotect AllowSorting = True
End Sub
It is a bit meaningless, but as far as you have used "AllowSorting = True" as a password, you can use it for the unprotect.

Related

Worksheet.Protect does not always apply the password

On Excel 2016, I would like to add a button in the Quick Access Toolbar that will toggle Protect/Unprotect the active worksheet, using always the same password.
I created an xlam file containing the following macro:
Sub Protection()
If ActiveSheet.ProtectContents Then
ActiveSheet.Unprotect Password:="TEST"
Else
ActiveSheet.Protect Password:="TEST"
End If
End Sub
It seems to work fine at first.
But when I do the following:
Protect using the regular Excel ribbon button with no password
Unprotect using the macro (it works even if no password is required and I think that's where the problem is)
Protect using the macro
Unprotect using the regular Excel button: No password is requested!!!
The following code seems to do the trick:
Sub Protection()
On Error Resume Next
If ActiveSheet.ProtectContents Then
ActiveSheet.Unprotect Password:=""
If Err.Number <> 0 Then
ActiveSheet.Unprotect Password:="TEST"
If Err.Number <> 0 Then
MsgBox ("The password is not the usual one.")
End If
End If
Else
ActiveSheet.Protect Password:="TEST"
End If
On Error GoTo 0
End Sub
However, it doesn't seem clean.
I have this problem on my work computer (Excel 2016) but it seems OK on my personal computer (Excel 365).
Is there a better way to make sure the correct protection is applied, whatever the user has done previously on the file?

Excel VBA protect returning error

I've seen many posts on here and elsewhere about using the following code in the ThisWorkbook Object in order to protect your workbook but still allow macros to run:
Private Sub Workbook_Open()
ActiveWorkbook.Protect UserInterfaceOnly:=True
End Sub
However, the protect function tooltip that pops up only offers three arguments Protect([password], [structure], [windows]). Therefore, when I open the document I get the following error
"Compile Error: Named argument not found"
in regard to UserInterfaceOnly:=True.
Am I using the wrong protect function, or is there something else I'm missing? I want to allow users to input data into the workbook using forms/macros, but I don't want them to be able to manually change any cells.
It almost seems as if the protect function that I'm running has different arguments than many of the other protect solutions I've found. Could this be a version issue or something else?
The suggestion provided by Tim Williams worked. Protect doesn't work the same way at the workbook level, therefore I needed to write individual code for each sheet I wanted to protect. I used the following code to protect the sheets but allow forms to populate:
Private Sub Workbook_Open()
Worksheets("Sheet1").Protect UserInterfaceOnly:=True
Worksheets("Sheet2").Protect UserInterfaceOnly:=True
Worksheets("Sheet3").Protect UserInterfaceOnly:=True
End Sub

Declare global variable for sheet password

I have a larger Excel file with multiple sheets and modules.
In the code for each of these I need to protect or unprotect a password protected sheet temporarily in order to update certain protected parts.
So far I use one of the following lines which works but this means that the password appears multiple times throughout the code.
Is there a way I can declare this password just once like a global variable and then just refer to this variable whenever needed so that it only has to be changed once if there is need for a change ?
Also, would this reduce security on the file ?
Current code:
To protect a sheet:
ActiveSheet.Protect Password:="MyPassword", UserInterfaceOnly:=True
To unprotect a sheet:
ActiveSheet.Unprotect Password:="MyPassword"
In your VB editor, right click on the project, and then Insert > Module
Call it something useful like 'Constants'
Insert the following statement:
Public Const strPwd as String = "MyPassword"
It is optional to type the constant, so the 'as String' part is down to taste. You can use this constant in any place in your project where you would previously have used your literal password string.
Regarding security, the best thing to do would be to make sure you have protected the VB project itself with a strong password. You can explore the options here in VB IDE > Tools > VBAProject Properties > Protection tab.
You can use this code as an example
Option Explicit
Public Const g_strPASSWARD As String = "MyPassword"
' To Protect
Sub ProtectSheet(ByRef shToProtect As Worksheet)
shToProtect.Protect Password:=g_strPASSWARD, UserInterfaceonly:=True
End Sub
'To Protect
Sub UnprotectSheet(ByRef shToUnprotect As Worksheet)
shToUnprotect.Unprotect Password:=g_strPASSWARD
End Sub
' To Use
Sub MyTest()
ProtectSheet ActiveSheet
UnprotectSheet ActiveSheet
End Sub
I hope this helps.

Hide specific sheets when closing workbook

I'm creating some VBA code which should do the following:
Users press a button a are required to input a code.
When the input the correct code the team relevant code they get access to certain sheets.
The sheets they get access to differs according to the team number and code they enter. So when they enter he password "banana": the sheets "Team_1" & Team_1_sub become visible.
I now created the following code to achieve this:
Sub filter_tabs()
Dim answer As String
answer = InputBox("Please enter your password")
If answer = "Password" Then
MsgBox "Correct, je krijgt nu de goede tabs te zien!"
Worksheets("Team_1").Visible = True
Worksheets("Team_1_sub").Visible = True
Else
MsgBox "Wrong password"
End If
End Sub
Two questions about the code above:
When the users close the document all sheet should "disappear" again. Does anybody know how to do this? So when opening the document sheets "Team_1" and "Team_1_sub" should be be standard
Worksheets("Team_1").Visible = False
Worksheets("Team_1_sub").Visible = False
Could you guys give me some feedback on whether the procedure I follow above (different if statements where users are prompted for a password and then get to see certain tabs) is the most efficient one to reach my goal? End goal is to make sure certain team leader can only see certain sheets.
Here for setting visible false, you can use Workbook_BeforeClose method as follow:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Worksheets("Team_1").Visible = False
Worksheets("Team_1_sub").Visible = False
'must save, if not save, it is not effect.
Me.Save
End Sub
Reference for that method is here.
One thing is that this method must have in the ThisWorkBook module.
For next question, you should say more like, which sheets for which user and password. Because you code is enough for your question. It can use for your requirement.
As you aren't using passwords you should at least make the sheets VeryHidden rather than Hidden - much harder for the average user to unhide.
The Me.Save proposed by the other answer also isn't necessary.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Worksheets("Team_1").Visible = VeryHidden
Worksheets("Team_1_sub").Visible = VeryHidden
End Sub
ad 1)
you best use a Workbook_BeforeSave() routine to code the hiding of all sheets ... in the VBAProject view you find this under ThisWorkbook
ad 2)
The code you post looks very nice - my point of concern would be the hard coding of user names vs. sheet names. I would consider putting this in a sheet/table using headers /code/ /sheetname/ ... this way you can adapt your logic at any time without having to modify the code.
With such a table at hand (in an all time hidden sheet if need be) you traverse it (one single piece of code) and - upon code entering - you unhide If CodeInTable = CodeEntered ... in the other case you unconditionally hide that sheet ... because hiding/unhiding differs only by 2 simple conditions.

Excel 2010 Macros Fun with Buttons

Ok so I am creating a spreadsheet that can be edited by another user but locked otherwise. What I am hoping to do is create 3 buttons. "What If" "Exit What if" and "Reset"
"What if" will allow for the user to input data.
"Exit what if" will allow for the user to exit the input mode and revert back to the default. document.
Then "Reset" will allow for the user stay in "What if" but reset all the values to default.
Then I want the button "What if" to appear somewhere up in the left but when you click it, its replaced by "Exit" and "Reset"
I suggest you to explain a little more you question, but so far for what I can infere you have the folloing issuse:
have a excel sheet with formulae and data locked.
offer edit the page, but no save the changes, as "a consult" to the data.
I can primarily offer the following:
Create a backup sheet where your save you base page
Unlock the sheet for editing.
if you exit the editing, restore the data from the backup to the main sheet.
if you reset the editing, do the same procedure as exit plus unlock again the data. (for how it was charted the code flow, the copied sheet has his data locked)
This will result in the followin code:
Sub BackUpData() 'this will be linked to you "what if" button
Sheets("Data_Sheet").Select 'select shhet with data, just in case
Range("A1:M56").Select ' range of your important data in your excel sheet
Cells.Select
Selection.Copy
Sheets("BackUp_Sheet").Select
Range("A1").Select 'lets paste the data in the same positiĆ³n
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Data_Sheet").Select
End Sub
This make a copy of the data and the formulas, copying charts without breaking his datasource is another problem, maybe your can elabore on this matter. Have any charts?
Sub RestoreData() 'this will be linked to you "Reset" and "Exit" button
Sheets("BackUp_Sheet").Select 'select shhet with data, just in case
Range("A1:M56").Select ' range of your important data in your excel sheet
Cells.Select
Selection.Copy
Sheets("Data_Sheet").Select
Range("A1").Select 'lets paste the data in the same positiĆ³n
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Usual room for improvement:
Dinamicaly select the range, but no select all the sheet, because memory isssuses may arise. (I run out of resources when try to copy all the cell of excel 2007 in my laptop :P).
Remove flicker with Application.ScreenUpdating.
I haven`t check if this work when the *backup_Sheet* is hidden.
The other isssuse is unlock the data in the sheet.
Sub UnlockMySheet()
'password here won`t protect the business logic or the code from prying eyes, just the user from themselves
ActiveWorkbook.Unprotect
ActiveSheet.Unprotect
Range("D9,B13").Select ' select the editable cells
Selection.Locked = False
Selection.FormulaHidden = False
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveSheet.EnableSelection = xlUnlockedCells
ActiveWorkbook.Protect Structure:=True, Windows:=True
End Sub
Usual room for improvement:
Maybe I forgot the protect protocol and I`m just leaving the page exactly as it was. (sorry no time to proof this code).
Sugestion from stackoverflow collective mind.
and that is, for now