Protected worksheet preventing code running

Ironman

Board Regular
Joined
Jan 31, 2004
Messages
186
Hi

The below code runs as part of a Workbook Open event

Code:
Sub Fillcell(Color As Long, cValue As String)

    Application.EnableEvents = False
  
    With Selection
      .Font.Name = "Wingdings"
      .Font.Size = 12
      .Font.ColorIndex = 1
      .Value = cValue
      
      If ActiveSheet.Name = "Training Log" Then
        With Selection.Validation
          .Delete
          .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
          :=xlBetween
          .IgnoreBlank = True
          .InCellDropdown = False
          .InputTitle = ""
          .ErrorTitle = ""
          .InputMessage = "Double click for lifetime mileage total up to this date"
          .ErrorMessage = ""
          .ShowInput = True
          .ShowError = True
        End With
      End If
    
    End With
  
    With Selection.Interior
      .ColorIndex = Color
      .Pattern = xlSolid
    End With
  
    Application.EnableEvents = True
  
End Sub

However, it is interrupted at the below point by the protected status of the sheet:

Code:
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
          :=xlBetween
I'd be grateful for a couple of lines of code that will allow all the above code to run and resume protection once it has completed.

Many thanks!
 
Last edited:

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
Try this. Change "abc" by the password desired.

Code:
Sub Fillcell(Color As Long, cValue As String)


    Application.EnableEvents = False
  
[COLOR=#0000ff]    Activesheet.unprotect "abc"[/COLOR]


    With Selection
      .Font.Name = "Wingdings"
      .Font.Size = 12
      .Font.ColorIndex = 1
      .Value = cValue
      
      If ActiveSheet.Name = "Training Log" Then
        With Selection.Validation
          .Delete
          .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
          :=xlBetween
          .IgnoreBlank = True
          .InCellDropdown = False
          .InputTitle = ""
          .ErrorTitle = ""
          .InputMessage = "Double click for lifetime mileage total up to this date"
          .ErrorMessage = ""
          .ShowInput = True
          .ShowError = True
        End With
      End If
    
    End With
  
    With Selection.Interior
      .ColorIndex = Color
      .Pattern = xlSolid
    End With
  
[COLOR=#0000FF]    Activesheet.protect "abc"

[/COLOR]
    Application.EnableEvents = True
  
End Sub
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
You're welcome and thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,032
Messages
5,526,355
Members
409,697
Latest member
christopherlewis1620

This Week's Hot Topics

Top