Excel VBA - Remove protective sheet for cell selection (one click, NOT double click)

I am trying to create a macro in Excel 2010 that will protect my worksheet when I click (not double-click) a cell in a certain range, on the contrary, if I click elsewhere, I would like to protect my worksheet.

Sorry, I'm not a VBA expert.

I came up with this code:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("E9:E22,I9:I21,N9:N20,Q9:Q14")) Is Nothing Then

        ActiveSheet.Unprotect Password:="abc"

    ElseIf Intersect(Target, Range("E9:E22,I9:I21,N9:N20,Q9:Q14")) Is Nothing Then

        ActiveSheet.Protect _
        Password:="abc", _
        UserInterfaceOnly:=True, _
        AllowFiltering:=True, _
        AllowSorting:=True, _
        AllowUsingPivotTables:=True

    End If
End Sub

This code works fine if, obviously, I double-click on a cell, but I was wondering if there is a way to make it work if I only select (one click) a cell?

I tried to use ActiveCellinstead Target, but without success.

Thanks a lot Stefano

+4
source share
1 answer

, BeforeDoubleClick Event. Worksheet_SelectionChange.

+2

Source: https://habr.com/ru/post/1690073/


All Articles