Protect Workbook with macro-code

Mötley

Board Regular
Joined
Aug 3, 2011
Messages
93
Hello, I'm using macro-code to protect workbook.

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.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi,
from what i can see you are only re protecting in the if statement. try putting at the end ie:

Code:
Else
   Set checkWs = Nothing
   MsgBox "Uusi taulukko voidaan lisätä huomenna."
   End If
[B]ActiveWorkbook.Protect[/B]
End Sub
 
Upvote 0
Hmm.. I still need help with one thing, in this case I wanna protect only ONE cell, prevent users to modify it anyhow. How can I do this?
 
Upvote 0
as far as i know you can ctrl A to select all of the cells, right click and go to format cells, then on go to protection and untick protected. on the cell you wish to protect do the same but tick protected. then protect sheet in code all done
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top