Probleme mit Sheet().unprotect in VBA
08.06.2005 10:55:36
Jochen
nachdem ich hier schon viele nützliche Informationen gefunden habe, hoffe ich jemand kann mir auch bei meinem problem weiterhelfen.
Folgendes:
Ich habe ein Dokument, welches per VBA-Skript in abhängigkeit von einem Faktor in bestimmten Zellen die Bedingte formatierung löschen/erstellen soll.
Dies geht auch hervorragend. Problem ist nun aber, dass ich gerne die Zellen Schützen würde und nur die Zellen während des einsatzes vom VBA-Skript ungeschützt lassen würde.
Hierfür müssten theoretisch die Aufrufe:
Activesheet.unprotect passwort:="passwort"
und
Activesheet.protect passwort:="passwort"
reichen.
In meinem Aufruf funktioniert der 'protect'-befehl einwandfrei, innerhalb des vba-codes geht allerdings der 'unprotect'befehl nicht
Mach ich einen eigenständigen Button mit dem 'Unprotect'-Befehl geht das ganze auch wunderbar.
Nun zur Frage:
Sieht jemand im angehängten Code den Fehler?
Sub on_change_phase()
Dim val1 As Integer
Dim selrange As String
Dim comprange As String
Dim helpvar As String
'unprotect the worksheet for the macro DON'T USE THE PASSWORD FOR EDITING THE FILE
Sheets("WP Report").Activate 'dies scheint nicht zu funktionieren ...
ActiveSheet.Unprotect password:="dontuse" ' Warum?
'Sheets("WP Report").Unprotect password:="dontuse"
'ActiveSheet.Unprotect password:="dontuse"
val1 = Range("AX64").Value + 62
'reading the value of the dropdown-menue
helpvar = CStr(val1)
'helpvar = Integer to String (val1)
selrange = Range("AY" + helpvar).Value
'reading in the range of the cells which should be formated
comprange = Range("AZ64").Value
'reading in the complete range of all cells (for deleting the FormatConditions)
Call Phase(selrange, comprange)
ActiveSheet.Protect password:="dontuse"
End Sub
Function Phase(selrange, comprange)
Dim countdel As Integer
Dim count_i As Integer
Dim area As String
Dim helpvar2 As String
'unprotect the worksheet
'ActiveSheet.Unprotect password:="dontuse"
'next 2 lines: delete the FormatConditions (from previous use of the file)
Range(comprange).Select
Selection.FormatConditions.Delete
'next 10 lines: make new FormatConditions (only for selected cells/phases)
Range(selrange).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""-"""
Selection.FormatConditions(1).Interior.Pattern = xlNone
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual _
, Formula1:="0,95"
Selection.FormatConditions(2).Interior.ColorIndex = 4
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="0", Formula2:="0,949"
Selection.FormatConditions(3).Font.ColorIndex = 2
Selection.FormatConditions(3).Interior.ColorIndex = 3
'next 10 lines: delete the format (colour) of the gaps between the Phases which where formated before
counter_i = 0
countdel = Range("BB63").Value
Do
helpvar2 = CStr((63 + counter_i))
area = Range("BA" + helpvar2).Value
Range(area).Select
Selection.FormatConditions.Delete
counter_i = counter_i + 1
Loop Until counter_i = (countdel)
'protect the worksheet
'ActiveSheet.Protect Password:="dontuse"
End Function
Danke für die Antworten
Jochen