Hello, I'm using macro-code to protect workbook.
It works like this --> I protect workbook. I need to add sheet (you can't add if you have protected workbook), so I use code that allows me to add more sheets.
Problem is that every time I protect workbook and then run the code, it doesn't stay protected. Workbook IS protected but when I add one more sheet, protection goes off. Could you please help me what's wrong with my code, so that it stays protected when I add sheets etc etc.
Rich (BB code):
Sub UusiSivu()
ActiveWorkbook.Unprotect
Dim CurrentDay As Integer
Dim NewName As String
Dim WS As Worksheet
Set WS = ActiveSheet
If IsNumeric(Right(WS.Name, 2)) Then
CurrentDay = Right(WS.Name, 2)
ElseIf IsNumeric(Right(WS.Name, 1)) Then
CurrentDay = Right(WS.Name, 1)
Else
Exit Sub
End If
CurrentDay = CurrentDay + 1
NewName = Format(Date, "dd.mm.yyyy")
Dim checkWs As Worksheet
On Error Resume Next
Set checkWs = Worksheets(NewName)
If checkWs Is Nothing Then
'Copies the current sheet to the end of the workbook
Sheets("Default").Copy after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = NewName
Dim oleObj As OLEObject
Else
Set checkWs = Nothing
MsgBox "Uusi taulukko voidaan lisätä huomenna."
ActiveWorkbook.Protect
End If
End Sub
It works like this --> I protect workbook. I need to add sheet (you can't add if you have protected workbook), so I use code that allows me to add more sheets.
Problem is that every time I protect workbook and then run the code, it doesn't stay protected. Workbook IS protected but when I add one more sheet, protection goes off. Could you please help me what's wrong with my code, so that it stays protected when I add sheets etc etc.