Using macro to sign and lock spreadsheet - but failed!

Koala123

New Member
Joined
Apr 13, 2019
Messages
24
Office Version
  1. 365
Hi there. The below is done by a previous colleague and the purpose is to allow people to "click-sign" the spreadsheet, and after signed the spread is supposed to be locked and no one can change any data. Unless the original signer removes signature then sheet will be unprotected.

But there problem is after it's signed, the bank details sections (merged cells) are not locked, which means people are still able to change that part after sheet is signed. I believe it's not good so would like to fix this loophole.

Not sure how can I change the below coding to make the whole sheet locked after signing? Can anyone please help?

Thanks in advance!

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim A As Worksheet: Set A = ActiveSheet
    Dim Sign As String: Sign = Application.UserName & " - " & WorksheetFunction.Text(Now(), "DD MMM YYYY hh:mm:ss")
    
    'Detect if selected cell is a check cell
    If Not (Application.Intersect(Target, Range("Check.Prep")) Is Nothing) Then
        'Detect if the check cell has been signed already
        If ActiveCell.Text <> "" Then
            'Check cell is signed, provide dialogue box
            If Left(ActiveCell.Text, InStr(1, ActiveCell.Text, " - ") - 1) = Application.UserName Then
                If MsgBox("Do you want to remove this signature?", vbOKCancel, "Delete Signature") = vbOK Then
                    A.Unprotect "abc"
                    ActiveCell.Cells(1, 1).Value = ""
                    ActiveCell.Offset(1, 0).Select
                    'LockSheet A, "abc"
                End If
            Else
                MsgBox "Sorry, but signature can only be removed by the original signatory", vbInformation
            End If
        Else
            'Check cell is not signed, create electronic signature
            A.Unprotect "abc"
            ActiveCell.Value = Sign
            Dim i As Long: i = 0
            ActiveCell.Offset(1, 0).Select
            LockSheet A, "abc"
        End If
    End If
    Application.Calculation = xlCalcul
End Sub
Screenshot 2023-01-28 184346.jpg
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try adding ActiveSheet.Cells.Locked = True above the sheet protection such as the below.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim A As Worksheet: Set A = ActiveSheet
    Dim Sign As String: Sign = Application.UserName & " - " & WorksheetFunction.Text(Now(), "DD MMM YYYY hh:mm:ss")
    
    'Detect if selected cell is a check cell
    If Not (Application.Intersect(Target, Range("Check.Prep")) Is Nothing) Then
        'Detect if the check cell has been signed already
        If ActiveCell.Text <> "" Then
            'Check cell is signed, provide dialogue box
            If Left(ActiveCell.Text, InStr(1, ActiveCell.Text, " - ") - 1) = Application.UserName Then
                If MsgBox("Do you want to remove this signature?", vbOKCancel, "Delete Signature") = vbOK Then
                    A.Unprotect "abc"
                    ActiveCell.Cells(1, 1).Value = ""
                    ActiveCell.Offset(1, 0).Select
                    'LockSheet A, "abc"
                End If
            Else
                MsgBox "Sorry, but signature can only be removed by the original signatory", vbInformation
            End If
        Else
            'Check cell is not signed, create electronic signature
            A.Unprotect "abc"
            ActiveCell.Value = Sign
            Dim i As Long: i = 0
            ActiveCell.Offset(1, 0).Select
            '>>>This will lock all cells in worksheet
            ActiveSheet.Cells.Locked = True
            LockSheet A, "abc"
        End If
    End If
    Application.Calculation = xlcalcul
End Sub
 
Upvote 0
Try adding ActiveSheet.Cells.Locked = True above the sheet protection such as the below.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim A As Worksheet: Set A = ActiveSheet
    Dim Sign As String: Sign = Application.UserName & " - " & WorksheetFunction.Text(Now(), "DD MMM YYYY hh:mm:ss")
   
    'Detect if selected cell is a check cell
    If Not (Application.Intersect(Target, Range("Check.Prep")) Is Nothing) Then
        'Detect if the check cell has been signed already
        If ActiveCell.Text <> "" Then
            'Check cell is signed, provide dialogue box
            If Left(ActiveCell.Text, InStr(1, ActiveCell.Text, " - ") - 1) = Application.UserName Then
                If MsgBox("Do you want to remove this signature?", vbOKCancel, "Delete Signature") = vbOK Then
                    A.Unprotect "abc"
                    ActiveCell.Cells(1, 1).Value = ""
                    ActiveCell.Offset(1, 0).Select
                    'LockSheet A, "abc"
                End If
            Else
                MsgBox "Sorry, but signature can only be removed by the original signatory", vbInformation
            End If
        Else
            'Check cell is not signed, create electronic signature
            A.Unprotect "abc"
            ActiveCell.Value = Sign
            Dim i As Long: i = 0
            ActiveCell.Offset(1, 0).Select
            '>>>This will lock all cells in worksheet
            ActiveSheet.Cells.Locked = True
            LockSheet A, "abc"
        End If
    End If
    Application.Calculation = xlcalcul
End Sub
Hey Breynolds this has fixed the issue, thank you very much!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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