+ Reply to Thread
Results 1 to 3 of 3

How to make this code work even if the cells are locked and sheet is protected

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-19-2012
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    200

    How to make this code work even if the cells are locked and sheet is protected

    Hello!

    I have the following command that works fine, but it doesn't work when the worksheet is protected. Is there any way to make it work even when the sheet is "Protected"?

    
    Sub Clear()
    
    Dim vbResponse As VbMsgBoxResult
    
    vbResponse = MsgBox("Are you sure you want to clear data?", vbOKCancel, "Clear?")
    
       If vbResponse = vbOK Then
       Range("C7:E200").ClearContents
       Else
          Exit Sub
       End If
    
    End Sub
    I'm new to coding. So please provide an ENTIRE NEW CODE that I can just copy and replace with the old, not just bits to add to it.

    Thanks!

  2. #2
    Valued Forum Contributor
    Join Date
    04-30-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    547

    Re: How to make this code work even if the cells are locked and sheet is protected

    If you protect the sheet with VBA, then use the UserInterfaceOnly setting, so macros can change the sheet without unprotecting.

    	Sheets(1).Protect Password:="Secret", UserInterFaceOnly:=True
    Or, if you don't use code to protect the sheet, just unprotect it before the changes and then protect it again.

    
    Sub Clear()
    
    Dim vbResponse As VbMsgBoxResult
    
    vbResponse = MsgBox("Are you sure you want to clear data?", vbOKCancel, "Clear?")
    
       If vbResponse = vbOK Then
           Sheet1.Unprotect Password:="Secret"
           Range("C7:E200").ClearContents
           Sheet1.Protect Password:="Secret"
       Else
          Exit Sub
       End If
    
    End Sub
    Change the references to reflect your setup.

  3. #3
    Forum Contributor
    Join Date
    05-19-2012
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    200

    Re: How to make this code work even if the cells are locked and sheet is protected

    This worked... thank you very much!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1