How to set password for multiple sheets in vba? - vba

I need to set password for multiple excel sheets in vba. I have a excel document with sheets for different company sections. I want that when I open excel document that only start page is shown, and then when I eneter password , if it is correct, then to open only two sheets for that section. Idea is:
Open excel document, start page is shown.
Enter password for one of the 6 sections
If password for section1 or section 2 ... or section 4 is correct, show two sheets that belongs to section for which we entered password.
If password for section5 or section 6 is correct, then show all sheets. I've tried first to lock one sheet with this code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim xSheetName As String
xSheetName = "Sheet1"
If Application.ActiveSheet.Name = xSheetName Then
Application.EnableEvents = False
Application.ActiveSheet.Visible = False
xTitleId = "KutoolsforExcel"
response = Application.InputBox("Password", xTitleId, "", Type:=2)
If response = "123456" Then
Application.Sheets(xSheetName).Visible = True
Application.Sheets(xSheetName).Select
End If
End If
Application.Sheets(xSheetName).Visible = True
Application.EnableEvents = True
End Sub
Problem with this whenever I click on other sheet and then click back to sheet1 it asks for password ( becouse of
`If Application.ActiveSheet.Name = xSheetName Then
Application.EnableEvents = False
Application.ActiveSheet.Visible = False`
I suppose, but I don't know how to avoid it.
Then , when I tried to lock multiple sheets like this :
Dim xSheetName1 as String
Dim xSheetName3 as String
xSheetName1 = "Sheet1"
xSheetName3 = "Sheet3"
If Application.ActiveSheet.Name = xSheetName1 Then
Application.EnableEvents = False
Application.ActiveSheet.Visible = False
xTitleId = "KutoolsforExcel"
response = Application.InputBox("Password", xTitleId, "", Type:=2)
If response = "123" Then
Application.Sheets(xSheetName1).Visible = True
Application.Sheets(2).Visible = True
Application.Sheets(xSheetName1).Select
If Application.Sheets(1).Visible = True Then
Application.EnableEvents = False
Aplication.Sheets(xSheetName1).Visible = False
Exit Sub
End If
End If
End If
If Application.ActiveSheet.Name = xSheetName3 Then
Application.EnableEvents = False
Application.ActiveSheet.Visible = False
xTitleId = "KutoolsforExcel"
response = Application.InputBox("Password", xTitleId, "", Type:=2)
If response = "111" Then
Application.Sheets(xSheetName3).Visible = True
Application.Sheets(5).Visible = True
Application.Sheets(5).Select
If Application.Sheets(1).Visible = True Then ' tried to get rid of asking
for password every time
Application.EnableEvents = False
' Aplication.Sheets(xSheetName1).Visible = False
Exit Sub
End If
End If
End If
Application.Sheets(xSheetName1).Visible = True
Application.Sheets(xSheetName3).Visible = True
Application.EnableEvents = True
End Sub
then when I enter any correct password all locked sheets are unlocked.
Sometimes I've used Sheets(index) because for some sheets names error 9 makes problem :)))

I agree with batman, this is not a very secure way of doing this. Best to have this as a master file, and run the macro to split the sheets into multiple files with password.
This way you can still edit contents with 1 file, and still manage the security within 1 file.

Related

VBA - Application.DisplayAlerts = False not working

This is intended to be a daily macro that run to update a report. This file would need to overwrite the existing file daily. However, the Application.DisplayAlerts = False is not working and I still get the pop up saying that this file already exists and if I want to replace. Is there something wrong with my code or is there a workaround to use a method that would automatically click yes for me?
Sub DailyRefresh ()
'Open and refresh Access
Dim appAccess As Object
Set appAccess = GetObject("S:\Shared\DailyRefresh.accdb")
Application.DisplayAlerts = False
appAccess.Visible = True
appAccess.DoCmd.RunMacro "Run_All_Queries"
appAccess.CloseCurrentDatabase
'Open Excel
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open ("s:\Shared\Template.xlsx")
xl.Visible = True
Application.DisplayAlerts = False
'Set date to the 1st of the Month on Summary tab
xl.Sheets("Summary").Visible = True
xl.Sheets("Summary").Select
xl.Range("C10").Value = DateSerial(Year(Now), Month(Now), 1)
xl.Range("C10").NumberFormat = "mm/dd/yyyy"
' REFRESH Table
xl.Sheets("Data").Visible = True
xl.Sheets("Data").Select
xl.Range("A1").Select
xl.Range("DailyRefresh.accdb[[#Headers],[ACTIVITY_DT]]").Select
xl.Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
xl.Worksheets("Fname").Visible = True
xl.Sheets("Fname").Select
'Copy and Save AS
Application.DisplayAlerts = False
Path = "S:\Shared\NewTemplate"
Filename = xl.Sheets("Fname").Range("A7").Value
xl.SaveAs Path & Filename & ".xlsx", FileFormat:=51, CreateBackup:=False
xl.Worksheets("Fname").Visible = False
xl.Close
Application.DisplayAlerts = True
End Sub
Application.DisplayAlerts = False
refers to the application where your code is running, not the Excel instance you created.
xl.DisplayAlerts = False
would be what you want.
I propose you to use Application.DisplayAlerts = False exactly before the line(s) that are taking time (no other lines between them). I had the same problem, because I was using it just once, but after each line that calls an external app, when it is back to VBA, it goes back to true.

VBA hide sheets from specific users

Looking for some help on VBA User restrictions.
So far I have the code pasted below. It is working perfectly, but I want to build on it.
I have it so the specific users listed have access to the file, and anyone else who tries to access the file gets a msgbox saying they aren't authorized and then the book closes.
I am however hoping that some of the users can see some sheets (the sheets they shouldn't see will be xlveryhidden) And then the other users can see the other sheets listed...
ie:
Name 1 can see sheet 13,
Name2 can see sheet14 and sheet3
Name 3 can see sheet22 sheet23 and sheet4
In terms of security it isn't hugely important, they are all from the same team, but just for user friendly and tidy document.
Private Sub Workbook_Open()
Dim Users As Variant
Dim UName As String
Dim UFind As Variant
Users = Array("Name1", "Name2", "Name3", "Name4", "Name5")
UName = Environ("UserName")
On Error Resume Next
UFind = WorksheetFunction.Match(UName, Users, 0)
If Err <> 0 Then
MsgBox "You are not authorised to use this Workbook"
ThisWorkbook.Close SaveChanges:=False
End If
End Sub
Came up with an answer, it is pretty simple, and wont withstand new users being added, but for the mean time it is ok...
Private Sub Workbook_Open()
Dim Users As Variant
Dim UName As String
Dim UFind As Variant
Users = Array("Name1", "Name2", "Name3")
UName = Environ("UserName")
On Error Resume Next
UFind = WorksheetFunction.Match(UName, Users, 0)
If UName = "Name2" Then
Worksheets("Sheet23").Visible = True
Worksheets("SHEET17").Visible = True
ElseIf UName = "Name1" Then
Worksheets("Sheet23").Visible = True
Worksheets("SHEET17").Visible = True
Worksheets("Sheet4").Visible = True
ElseIf UName = "Name3" Then
Worksheets("Sheet23").Visible = True
Worksheets("SHEET17").Visible = True
ElseIf Err <> 0 Then
MsgBox "You are not authorised to use this Workbook"
ThisWorkbook.Close SaveChanges:=False
End If
End Sub
And in order to re-hide them all again when closing the file:
SubPrivate Sub Workbook_BeforeClose(Cancel As Boolean)
Worksheets("Sheet23").Visible = False
Worksheets("SHEET17").Visible = False
Worksheets("Sheet4").Visible = False
Worksheets("Sheet1").Visible = False
‘If you don’t save it’s not effective
Me.Save End Sub
Make changes in your If condition as:
If Err <> 0 Then
MsgBox "You are not authorised to use this Workbook"
ThisWorkbook.Close SaveChanges:=False
Else
For Each ws In Worksheets
If ws.Name <> "Sheet" & UFind Then
ws.Visible = xlSheetHidden
End If
Next ws
End If
Make sure that the sheet names are Sheet1, Sheet2, Sheet3, .. etc as mentioned in the question.

Vba macro excel: How to hide rows if cell equal FALSE

I have a project which requires Excel to hide rows on a separate sheet(within the same workbook) after user selects specific options on the activesheet. The macro is linked to a button, when clicked rows will be hidden on the separate sheet, and the whole process occurs in the background. If the user want to check the table with hidden rows they'd need to navigate to that separate sheet to see the result.
Image explanation:
http://postimg.org/image/ek6981vg1/
Worksheets("Input- Select Pens") --> active sheet where has the button
Worksheets("Input- Pen") --> separate sheet where has the hidden rows
I have tried several methods, but none of them worked:
Method 1:
Sub selectPens()
Dim c As Range
Application.EnableEvents = False
On Error Resume Next
For Each c In Range("E6:E35")
If c.Value = "FALSE" Then
Worksheets("Input- Pen").c.EntireRow.Hidden = True
ElseIf c.Value = "TRUE" Then
Worksheets("Input- Pen").c.EntireRow.Hidden = False
End If
Next c
On Error GoTo 0
Application.EnableEvents = True
End Sub
Method 2:
Sub selectPens()
Dim i As Long
Set wselect = Sheet11
With wselect
For i = 6 To 35
If ActiveSheet.Cells(i, 5).Value = "FALSE" Then
.Range("i:i").EntireRow.Hidden = True
' .Rows(i).EntireRow.Hidden = True
ElseIf ActiveSheet.Cells(i, 5).Value = "TRUE" Then
' .Rows(i).EntireRow.Hidden = False
.Range("i:i").EntireRow.Hidden = False
End If
Next i
End With
End Sub
I would be greatly appreciated for any help.
Many thanks!
Sub selectPens()
Dim i As Long, wsselect
Set wselect = Sheet11
For i = 6 To 35
'EDIT
wselect.Rows(i).Hidden = (ActiveSheet.Cells(i, 5).Value = False)
Next i
End Sub

How can I get my Macro to run on cell selection?

I am not new to programming, but I am new to using macros in Excel. I am using Excel 2010, trying to run the following macro:
Sub HideUnhideCells(ByVal Target As Range)
Dim keyCell As Range
Set keyCell = Range("C9")
Dim Cells1 As Range
Dim Cells2 As Range
'Call the function on C9 cell change
If Target.Address = "$C$9" Then
'Make Data Source available for for DRG and UCR
If keyCell.Value = "DRG" Or keyCell.Value = "UCR" Then
Set Cells1 = Range("C33")
Cells1.EntireRow.Hidden = False
Else
Set Cells1 = Range("C33")
Cells1.EntireRow.Hidden = True
End If
'Make MSA special cells available if MSA is selected
If keyCell.Value = "MSA" Then
Set Cells1 = Range("B34:C35")
Cells1.EntireRow.Hidden = False
Else
Set Cells1 = Range("B34:C35")
Cells1.EntireRow.Hidden = True
End If
'Make UCR cells available if UCR is selected
If keyCell.Value = "UCR" Then
Set Cells1 = Range("B36:C39")
Cells1.EntireRow.Hidden = False
Else
Set Cells1 = Range("B36:C39")
Cells1.EntireRow.Hidden = True
End If
'Remove extra name cells for 1-file and 2-file values
If keyCell.Value = "DRG" Or keyCell.Value = "ICD-9" Or keyCell.Value = "NCCI_Edits" Or keyCell.Value = "UB04" Then
Set Cells1 = Range("B21:C25")
Set Cells2 = Range("B28:C32")
Cells1.EntireRow.Hidden = True
Cells2.EntireRow.Hidden = True
ElseIf keyCell.Value = "ICD-10" Or keyCell.Value = "NDC" Then
Set Cells1 = Range("B22:C25")
Set Cells2 = Range("B29:C32")
Cells1.EntireRow.Hidden = True
Cells2.EntireRow.Hidden = True
Else
Set Cells1 = Range("B21:C25")
Set Cells2 = Range("B28:C32")
Cells1.EntireRow.Hidden = False
Cells2.EntireRow.Hidden = False
End If
End If
End Sub
I have seen several postings and tutorials that talk about this, but I can't understand why this won't work. Cell C9 is a dropdown list, and I want this macro to run so that cells are shown / not shown based on what is selected in the list. However, if I give it parameters (as shown above) I can't run it in the UI, and if I don't give it parameters, I can only run it manually, which doesn't help me much.
Right now, when I select something from that C9 dropdown list, nothing happens. Can anyone help me figure out why?
Your code looked ripe for a Select Case treatment and there were several things to add about the Worksheet_Change event macro (too many for a comment) so I went ahead and wrote up a draft of the Sub Worksheet_Change. I'm not sure if I have interpreted all of the If ElseIf Else End If but perhaps you can see what I'm trying to do with this.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$9" Then
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error GoTo Whoa
Rows("21:25").EntireRow.Hidden = False
Rows("28:32").EntireRow.Hidden = False
Rows("33:39").EntireRow.Hidden = True
Select Case Target.Value
Case "DRG"
Rows("33").EntireRow.Hidden = False
Case "MSA"
Rows("34:35").EntireRow.Hidden = False
Case "UCR"
Rows("33").EntireRow.Hidden = False
Rows("36:39").EntireRow.Hidden = False
Case "DRG", "ICD-9", "NCCI_Edits", "UB04"
Rows("21:25").EntireRow.Hidden = True
Rows("28:32").EntireRow.Hidden = True
Case "ICD-10", "NDC"
Rows("22:25").EntireRow.Hidden = True
Rows("29:32").EntireRow.Hidden = True
Case Else
'do nothing
End Select
End If
FallThrough:
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume FallThrough
End Sub
Post back into Comments with any problem you have transcribing this for your own purposes and I'll try to assist.

Using VBA to set passwords to edit different ranges

I am trying to write a code that will ask for a password to open an Excel file. There should be three passwords (e.g. America, Asia, Europe). Depending on the password entered, only certain range should be enabled for editing (e.g. "America" for A2:A100, "Asia" for B2:B100, "Europe" for C2:C100).
Set up your workbook as follows:
select each of your ranges and assign them a name - America, Asia and Europe
Add the following code to the workbook
Private Sub Workbook_Open()
Dim password As String
password = InputBox("Password", "Please enter the password")
Dim worksheetpassword As String
worksheetpassword = "password"
If password = "AmericaPassword" Then
ActiveSheet.Unprotect (worksheetpassword)
ActiveSheet.Range("America").Locked = False
ActiveSheet.Range("Asia").Locked = True
ActiveSheet.Range("Europe").Locked = True
ActiveSheet.Protect (worksheetpassword)
ElseIf password = "AsiaPassword" Then
ActiveSheet.Unprotect (worksheetpassword)
ActiveSheet.Range("America").Locked = True
ActiveSheet.Range("Asia").Locked = False
ActiveSheet.Range("Europe").Locked = True
ActiveSheet.Protect (worksheetpassword)
ElseIf password = "EuropePassword" Then
ActiveSheet.Unprotect (worksheetpassword)
ActiveSheet.Range("America").Locked = True
ActiveSheet.Range("Asia").Locked = True
ActiveSheet.Range("Europe").Locked = False
ActiveSheet.Protect (worksheetpassword)
Else
ActiveSheet.Unprotect (worksheetpassword)
ActiveSheet.Range("America").Locked = True
ActiveSheet.Range("Asia").Locked = True
ActiveSheet.Range("Europe").Locked = True
ActiveSheet.Protect (worksheetpassword)
MsgBox ("You cannot edit this file")
End If
End Sub
Password protect your workbook - the code assumes a password of "password"
Close the workbook and reopen, you will be prompted for a password. If you enter "AmericaPassword" you should be able to modify the "America" range, "AsiaPassword" to modify the "Asia" range etc.