Is it possible to protect a workbook that has VBA codes written in it?

gruhnelliot

New Member
Joined
Jun 4, 2013
Messages
14
I have a workbook that has 6 different worksheets that you log into with a password specific for each sheet. 5 of the worksheets are for user interface and the 6th worksheet is for admin. I would like to protect the workbook so the users have rights to enter data, but not add or delete worksheets. Is this possible? Every VBA code I have tried doesn't work.

This is the code I have for the workbook:
rivate Sub Workbook_Open()
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",false)"
Application.DisplayFormulaBar = False

Dim wksuserlog As Worksheet
Dim lngnextrow As Long


On Error Resume Next
Set wksuserlog = ThisWorkbook.Worksheets("User Log")

On Error GoTo ExitProc
If wksuserlog Is Nothing Then
Set wksuserlog = ThisWorkbook.Worksheets.Add
wksuserlog.Name = "User Log"
wksuserlog.Range("a1:b1").Value = Array("Username", "Date_time")
End If

With wksuserlog
lngnextrow = .cells(.Rows.Count, "a").End(xlUp).Row + 1
.cells(lngnextrow, "a").Value = Environ("Username")
.cells(lngnextrow, "b").Value = Now()
.Columns("a:b").AutoFit
End With

ExitProc:

Dim Ws As Worksheet

Application.DisplayAlerts = False
Application.ScreenUpdating = False

For Each Ws In ActiveWorkbook.Worksheets

If Ws.Name <> "Login" Then
Ws.Visible = xlSheetVeryHidden
End If
Next Ws

LoginUF.Show


End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
End Sub
 

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

rawr19911

New Member
Joined
Jan 21, 2020
Messages
41
Office Version
  1. 2016
I have a workbook that has 6 different worksheets that you log into with a password specific for each sheet. 5 of the worksheets are for user interface and the 6th worksheet is for admin. I would like to protect the workbook so the users have rights to enter data, but not add or delete worksheets. Is this possible? Every VBA code I have tried doesn't work.
does it have to be a VBA code that does it - just as easy to manually do it -

To have it to where they can only change the data or enter data- u can unlock cells u want them to be able to update and lock the work page down- right clicking tab protect worksheet-
for workbook- go to file protect workbook and structure so they cant add and remove tab's (sheets)
 

gruhnelliot

New Member
Joined
Jun 4, 2013
Messages
14
That doesn’t work. When I protect the workbook or worksheet, the VBA codes that create the password protected login screen generate an error. Then the workbook can be opened without the password.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,862
Messages
5,574,713
Members
412,613
Latest member
EFRATA
Top