Consulting

Results 1 to 4 of 4

Thread: Protect & Unprotect Sheet in AutoCall Macro

  1. #1
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    2
    Location

    Question Protect & Unprotect Sheet in AutoCall Macro

    Hi Friends,

    any solution for my case?

    im trying to creat auto call macro (simple solver - in another module) with this code :

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim KeyCells As Range
    Set KeyCells = Range("$D$15:$F$19")

    Run "macro1"

    Sheets("sheet1").Select
    ActiveSheet.Unprotect Password:="love"


    End Sub

    Its works to autocall macro (with password : love), when im change the row from D15 : F19 with ENTER
    But after ENTER i want the sheet protected again

    i had try to add this code before End Sub :
    Sheets("sheet1").Select
    ActiveSheet.Protect Password:="love"


    but still failed. Anyone help me ?

    thank you

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Welcome to the forum! Normally,

    I set Protect with the UserInterfaceOnly option so all my code can change sheet objects. In ThisWorkbook object:
    Private Sub Workbook_Open()  
      Dim ws As Worksheet
      For Each ws In Worksheets
        ' UserInterfaceOnly:=True allows code to change data.
        ws.Protect "ken", UserInterfaceOnly:=True, DrawingObjects:=True, _
          Contents:=True, Scenarios:=True
      Next ws
    End Sub
    Change ken to suit. Or, just do it for one sheet there or at the beginning of your macro. Then you don't have to worry about Unprotect/Protect each time.

  3. #3
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    2
    Location
    Quote Originally Posted by Kenneth Hobs View Post
    Welcome to the forum! Normally, I set Protect with the UserInterfaceOnly option so all my code can change sheet objects. In ThisWorkbook object:
    Private Sub Workbook_Open()    Dim ws As Worksheet  For Each ws In Worksheets    ' UserInterfaceOnly:=True allows code to change data.    ws.Protect "ken", UserInterfaceOnly:=True, DrawingObjects:=True, _      Contents:=True, Scenarios:=True  Next wsEnd Sub
    Change ken to suit. Or, just do it for one sheet there or at the beginning of your macro. Then you don't have to worry about Unprotect/Protect each time.
    Hi Ken, Thank you for your feedback.. But im still failed use you suggestion code..Could you share sample excel file for your code..?Thank you

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Not much to it....
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •