Unhide Columns With A Password

J

Jim

I am trying to create a small marco that unhides a few columns in excel, but
prompts for a password. Is this possible?

Thanks
 
G

Gord Dibben

It is possible with an inputbox.

Sub unhide()
pword = InputBox("enter the password")
If pword <> "mypass" Then
MsgBox "incorrect password"
Exit Sub
Else
ActiveSheet.Range("A:A,C:C,D:D").EntireColumn.Hidden = False
End If
End Sub

BUT...........without protecting the sheet, placing a password on a macro
would prove to be sort of useless.

Users could unhide the columns without using your macro.

Protect the sheet with columns hidden and a password to unprotect.

Users will have to know the password.

Also be warned that Excel's internal passwords are eaty to crack.


Gord Dibben MS Excel MVP
 
J

Jim

Thanks for the reply. I think I have come up with a solution by using a
macro to unhide, but prompting to unlock the sheet with a password.

So, to hide the given cells, user hits a button with a macro, this hides the
relevant columns and locks the sheet. I does allow the user to format cells
etc.

Then the second button executes second macro, and this then prompts a
password to unprotect the sheet and then unhides. Now I just need to hide
the code from any sneaky users!

Macro 1

Columns("L:O").Select
Selection.EntireColumn.Hidden = True
Range("A1").Select

ActiveSheet.EnableOutlining = True
ActiveSheet.Protect Password:="password", _
DrawingObjects:=True, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=False, _
AllowFormattingRows:=True, AllowInsertingColumns:=True,
AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True,
_
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True

Macro 2

ActiveSheet.Unprotect
Columns("K:p").Select
Selection.EntireColumn.Hidden = False
Range("A1").Select
 
G

Gord Dibben

Alt + F11 to go to VBE

Select your workbook/project.

Right-click>VBAProject Properties>Protection>Lock for viewing.

Enter a password twice and don't forget it.

Save and close workbook.


Gord
 
J

Jim

Thanks Gord

Gord Dibben said:
Alt + F11 to go to VBE

Select your workbook/project.

Right-click>VBAProject Properties>Protection>Lock for viewing.

Enter a password twice and don't forget it.

Save and close workbook.


Gord
 
Top