Modifying a VBA code that is meant to lock a sheet/adding in a password requirement

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
886
Office Version
  1. 365
Platform
  1. Windows
I was assisted with the below code on this Forum but need further help with it.

The purpose of this code was to disable a excel spreadsheet (close the sheet) if the sheet was copied to another location. This did not have to be fool proof as the users are not well versed in Excel and it is more meant as a deterrent for people making their own copies.

That being said if a copy is made and the directory of the workbook does not contain the file name referenced in Sheets("List").Range("F1") the workbook closes.

What I would like is if it doesnt match, instead of closing the workbook lock it down with a password instead.

Is this possible?

Thank you to anyone who can help.

Batch Log.xlsm
EF
1First file in Directory:AEROSPACE
2\\DAVIN\CADO\AEROSPACE\Batch Records\Batch Log.xlsm
List


VBA Code:
Private Sub Workbook_Open()

Application.Calculation = xlAutomatic

Application.CalculateBeforeSave = True

Dim firstFolder As String

Dim fn1 As String

Dim fn2 As String

Dim FullFileName As String

Const AllowCancel = False

FullFileName = Sheets("List").Range("F2").Value

firstFolder = Sheets("List").Range("F1").Value

With ActiveWorkbook



On Error Resume Next



fn1 = Mid(.FullName, InStr(UCase(.FullName), UCase(firstFolder)))

fn2 = Mid(FullFileName, InStr(UCase(FullFileName), UCase(firstFolder)))

If fn1 <> fn2 Then

Dim choice As Long, bttns As Long

If AllowCancel Then bttns = vbOKCancel Else bttns = vbOKOnly

choice = MsgBox("This is a copy of the original workbook and therefore cannot be used. Please open the correct workbook.", bttns)

If choice = vbOK Then .Close False



End If

End With

End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Check if this is what you need. The password is "willow"

VBA Code:
Private Sub Workbook_Open()
  Application.Calculation = xlAutomatic
  Application.CalculateBeforeSave = True
  Dim firstFolder As String
  Dim fn1 As String
  Dim fn2 As String
  Dim FullFileName As String
  Const AllowCancel = False
  FullFileName = Sheets("List").Range("F2").Value
  firstFolder = Sheets("List").Range("F1").Value
  With ActiveWorkbook
    On Error Resume Next
    fn1 = Mid(.FullName, InStr(UCase(.FullName), UCase(firstFolder)))
    fn2 = Mid(FullFileName, InStr(UCase(FullFileName), UCase(firstFolder)))
    If fn1 <> fn2 Then
      Dim choice As Long, bttns As Long
      If AllowCancel Then bttns = vbOKCancel Else bttns = vbOKOnly
      choice = MsgBox("This is a copy of the original workbook and therefore cannot be used. Please open the correct workbook.", bttns)
      If choice = vbOK Then
        Application.DisplayAlerts = False
        .SaveAs Filename:=.FullName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="willow", CreateBackup:=False
        .Close False
      End If
    End If
  End With
End Sub
 
Upvote 0
Check if this is what you need. The password is "willow"

VBA Code:
Private Sub Workbook_Open()
  Application.Calculation = xlAutomatic
  Application.CalculateBeforeSave = True
  Dim firstFolder As String
  Dim fn1 As String
  Dim fn2 As String
  Dim FullFileName As String
  Const AllowCancel = False
  FullFileName = Sheets("List").Range("F2").Value
  firstFolder = Sheets("List").Range("F1").Value
  With ActiveWorkbook
    On Error Resume Next
    fn1 = Mid(.FullName, InStr(UCase(.FullName), UCase(firstFolder)))
    fn2 = Mid(FullFileName, InStr(UCase(FullFileName), UCase(firstFolder)))
    If fn1 <> fn2 Then
      Dim choice As Long, bttns As Long
      If AllowCancel Then bttns = vbOKCancel Else bttns = vbOKOnly
      choice = MsgBox("This is a copy of the original workbook and therefore cannot be used. Please open the correct workbook.", bttns)
      If choice = vbOK Then
        Application.DisplayAlerts = False
        .SaveAs Filename:=.FullName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="willow", CreateBackup:=False
        .Close False
      End If
    End If
  End With
End Sub

This code works unfortunately when moved to another location it prompts for a password to re-open but then immediately closes again once the password is entered. I was hoping once the password is entered would give a chance to modify the workbook. This is close but the purpose of the password is if someone moves the original document then I could open it, enter the password and fix the document without having to disable macros.

Any ideas?

Thank you so much Dante!
 
Upvote 0
:unsure: I tried the code, save the file with password willow. I open the workbook, enter the password willow, macros are enabled, and I can modify the data. I don't know what problem you may have, maybe the version.
 
Upvote 0
:unsure: I tried the code, save the file with password willow. I open the workbook, enter the password willow, macros are enabled, and I can modify the data. I don't know what problem you may have, maybe the version.
If the workbook is in the wrong directory however and macros are enabled it wont let me modify the book because it automatically closes. Even in the wrong directory, when I enter the password I do not want it to close but have the option to change the directory in the workbook. I hope I am explaining this ok...
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,208
Members
448,874
Latest member
b1step2far

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top