Excel vba Fehler: Laufzeitfehler 1004 Die Locked-Eigenschaft des Range-Objektes kann nicht festgelegt werden
Fehlermeldung:
Microsoft Visual Basic Laufzeitfehler 1004 Die Locked-Eigenschaft des Range-Objektes kann nicht festgelegt werden |
Fehler vba Code-Zeile
ws.Cells(1, 1).Locked = True |
Prüfung ob eine Excel Blatt schon geschützt ist oder im Schutzmodus ist
If ws.ProtectContents = True Or ws.ProtectDrawingObjects = True Or ws.ProtectScenarios = True Then ws.Unprotect °const_Password End If |
Vba makro code: man muss ProtectContents, ProtectDrawingObjects und protectScenarios prüfen um den aktuellen Blattschutz zu ermitteln
Public Function Protect_Worksheet(ByVal ws As Worksheet) '--------< Protect_Worksheets() >-------- '*protect worksheet If ws.Visible = xlSheetVisible Then '*protect all visible worksheets
'< check: isProtected > If ws.ProtectContents = True Or ws.ProtectDrawingObjects = True Or ws.ProtectScenarios = True Then ws.Unprotect °const_Password End If '</ check: isProtected >
'< worksheet.Protect > ws.Protect °const_Password, Contents:=True _ , DrawingObjects:=False, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingHyperlinks:=True _ , Scenarios:=True, UserInterfaceOnly:=True, AllowInsertingColumns:=False, AllowInsertingRows:=False _ , AllowDeletingColumns:=False, AllowDeletingRows:=False _ , AllowSorting:=False, AllowFiltering:=True, AllowUsingPivotTables:=True '< worksheet.Protect > End If '--------</ Protect_Worksheets() >-------- End Function |
Problem:
Das Problem ist, dass das Arbeitsblatt gesperrt ist, aber die Abfrage mit worksheet.ProtectionMode=False tritt nie ein.
Denn worksheet.ProtectionMode zeigt nur an ob Frontpage und code-modul zusammen geschützt werden.
.ProtectionMode
says whether "User-Interface-only" protection
is turned on
'< check: Protected > '*wenn das Blatt geschuetzt ist, kann der Zell-Schutz nicht aktiviert werden Dim IsProtected As Boolean IsProtected = False If ws.ProtectionMode = True Then ws.Unprotect °const_Password IsProtected = True End If '</ check: Protected > |
Lösung:
Man kann das Excel Arbeitsblatt zuvor ensperren
ActiveWorksheet.Unprotect "111" |
Problem im Vba Debugger (makro-code)
Der vba Debugger sagt zur Laufzeit, dass das Blatt nicht gesperrt sei
- : Protection : : Protection/Protection : modSchutz.Schutz_Zellen_Sperren_nach_Muster_in_Bla
: ProtectionMode : Falsch : Boolean : modSchutz.Schutz_Zellen_Sperren_nach_Muster_in_Bla
Vba Code
Gesperrter Code lässt die Zeile zelle(xx).Locked=True nicht zu
Public Function Schutz_Zellen_Sperren_nach_Muster_in_Blatt(ByVal wb As Workbook, ByVal ws As Worksheet) '-----------------< Schutz_Zellen_Sperren_nach_Muster_in_Blatt() >----------------- 'On Error Resume Next '< check: abbruch > If Not ws.Visible = xlSheetVisible Then 'nur sichtbare seiten Exit Function End If '</ check: abbruch >
'< check: Protected > '*wenn das Blatt geschuetzt ist, kann der Zell-Schutz nicht aktiviert werden Dim IsProtected As Boolean IsProtected = False If ws.ProtectionMode = True Then ws.Unprotect IsProtected = True End If '</ check: Protected >
Application.StatusBar = Now & " Start: Zellen sperren in Blatt " & ws.Name
'vSheet.Activate Dim range_Cells As Range Set range_Cells = Nothing
Dim cell As Range For Each cell In ws.UsedRange.Cells
' Application.StatusBar = Now & " check locked " & ws.Name & "." & cell.Address ' DoEvents If cell.HasFormula Then '-< Ist_Zelle_mit_Formel >- If Not cell.Locked = True Then cell.Locked = True '-</ Ist_Zelle_mit_Formel >- End If Next
ws.Cells(1, 1).Locked = True
'< Protected_anpassen > Protect_Worksheet ws '</ Protected_anpassen > '-----------------</ Schutz_Zellen_Sperren_nach_Muster_in_Blatt() >----------------- End Function |