Desbloquear automaticamente libro compartido y protegido

Tengo un excel compartido, el cual quiero proteger y compartir al mismo tiempo. Al tener una macro con distintas funcionalidades ( la uso para bloquear columnas y generar fechas de registros en las columnas contiguas) no corre.

Quiero proteger y compartir:

adjunto una captura de la macro y donde falla:

Es al poner ActiveWorkbook. ExclusiveAccess cuando da el error. He intentado poner para que desbloquue antes con la función:

ActiveWorkbook. Unprotect "abc" que es la contraseña que le he dado para desbloquear el compartir el libro, pero aun así sigue dando error.

Si no le pongo contraseña y solo comparto la macro que tengo funciona sin problemas

Respuesta
1

Primero que nada, tu evento "change" está, en la práctica, activo todo el tiempo, sugiero cambiar el orden de tu código, algo así:

1- Primero el "If" del "Intersect"

2- Segundo el "If" del "MultiUserEditing"

3- Desproteger la hoja

Eso para comenzar. Lo otro es ¿qué mensaje de error te sale? ¿Estás completamente seguro que la línea que se marca con el error es la que has mencionado?

Comentas

Abraham Valencia

Te adjunto la macro entera:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.DisplayAlerts = False
If ActiveWorkbook.MultiUserEditing Then
ActiveWorkbook.UnprotectSharing "abc"
ActiveWorkbook.ExclusiveAccess

If Not Intersect(Target, Columns("K")) Is Nothing Then
ActiveSheet.Unprotect "abc"
For Each c In Target
Cells(c.Row, "L") = Date & " " & Time
Cells(c.Row, "L").Locked = True
Next
ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
If Not Intersect(Target, Columns("M")) Is Nothing Then
ActiveSheet.Unprotect "abc"
For Each c In Target
Cells(c.Row, "N") = Date & " " & Time
Cells(c.Row, "N").Locked = True
Next
ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If

If Not Intersect(Target, Columns("P")) Is Nothing Then
ActiveSheet.Unprotect "abc"
For Each c In Target
Cells(c.Row, "Q") = Date & " " & Time
Cells(c.Row, "Q").Locked = True
Next
ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
If Not Intersect(Target, Columns("R")) Is Nothing Then
ActiveSheet.Unprotect "abc"
For Each c In Target
Cells(c.Row, "S") = Date & " " & Time
Cells(c.Row, "S").Locked = True
Next
ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
If Not Intersect(Target, Columns("U")) Is Nothing Then
ActiveSheet.Unprotect "abc"
For Each c In Target
Cells(c.Row, "V") = Date & " " & Time
Cells(c.Row, "V").Locked = True
Next
ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
If Not Intersect(Target, Columns("W")) Is Nothing Then
ActiveSheet.Unprotect "abc"
For Each c In Target
Cells(c.Row, "X") = Date & " " & Time
Cells(c.Row, "X").Locked = True
Next
ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
If Not Intersect(Target, Range("A:J")) Is Nothing Then
ActiveSheet.Unprotect "abc"
Target.Locked = True

ActiveSheet.Protect

ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
If Not Intersect(Target, Range("Y:Y")) Is Nothing Then
ActiveSheet.Unprotect "abc"
Target.Locked = True

ActiveSheet.Protect

ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
If Not Intersect(Target, Range("AA:AD")) Is Nothing Then
ActiveSheet.Unprotect "abc"
Target.Locked = True

ActiveSheet.Protect

ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
If Not Intersect(Target, Range("O:O")) Is Nothing Then
ActiveSheet.Unprotect "abc"
Target.Locked = True

ActiveSheet.Protect

ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
If Not Intersect(Target, Range("T:T")) Is Nothing Then
ActiveSheet.Unprotect "abc"
Target.Locked = True

ActiveSheet.Protect

ActiveSheet.Protect "abc", _
DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, _
AllowFormattingColumns:=True, AllowFormattingRows:=True, _
AllowInsertingColumns:=True, AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, _
AllowFiltering:=True, AllowUsingPivotTables:=True
End If
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.FullName, accessMode:=xlShared
Application.DisplayAlerts = True
End If
End Sub

y ahora una captura del error

y al picar en depurar donde me direcciona:

Creo que mi problema esta en no volver a bloquear y compartir la hoja

Primero coloca en "Modo Exclusivo" y después desprotege, si intentas desproteger primero te dará error pues los "libros compartidos" no lo permiten. Es una de sus, varias, limitaciones.

Abraham Valencia

Buenas,

Perdón por el retraso en contestar. He probado la opción que me sugieres, eliminando directamente "ActiveWorkbook.ExclusiveAccess" y para la primera celda que escribo algo no hay problema, pero al intentar hacer otra me sale de nuevo un error ( adjunto captura):

 Me temo que es debido ha que en el final de la macro no se darle la orden de volver a compartir y proteger por lo que al iniciarse de nuevo vuelve a dar error. Hay alguna forma de darle esta indicación? adjunto como tengo yo el final y el comienzo:

 y el final:

Application.DisplayAlerts = False
'evaluamos si el libro esta compartido
If ActiveWorkbook.MultiUserEditing Then
'si esta compartido, lo ponemos en modo exclusivo para hacer lo que necesites, incluido desproteger hojas 
ActiveWorkbook.ExclusiveAccess
'la bla bla
'guardamos el libro nuevamente como compartido
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.FullName, _
accessMode:=xlShared
End If
'activamos alertas
Application.DisplayAlerts = True

Listo

Abraham Valencia

Buenas,

Esas son las ordenes iniciales que tenía. Mi problema viene causado porque además de compartir el libro quiero poner una contraseña a esa protección

Por eso daba la orden inicial de:

ActiveWorkbook.UnprotectSharing "abc"

El problema surgía al ir a cambiar un segunda celda, que al tener las ordenes:

ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.FullName, _
accessMode:=xlShared
End If
Application.DisplayAlerts = True

Lo que hace es guardarlo como compartido pero sin contraseña, por lo que al volver a comenzar y encontrarse con el "ActiveWorkbook.UnprotectSharing "abc" da un error.

ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.FullName, Password:= _
        "tu_clave", CreateBackup:=True, AccessMode:=xlShared

Comentas

Abraham Valencia

Lo del "CreateBackup" no lo coloques.

Abraham Valencia

Buenas,

Sigue sin funcionar, me sigue saliendo un error, así es como ha quedado:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.DisplayAlerts = False
If ActiveWorkbook.MultiUserEditing Then
ActiveWorkbook.UnprotectSharing "abc"
ActiveWorkbook.ExclusiveAccess

( coloco indistintamente el exclusiveaccess antes y después de unprotect y el error salta igual)

ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.FullName, Password:=_
"abc", AccessMode:=xlShared
End If
Application.DisplayAlerts = True

El error que salta es:

Si elimino de la función el exclusiveAccess:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.DisplayAlerts = False
If ActiveWorkbook.MultiUserEditing Then
ActiveWorkbook.UnprotectSharing "abc"
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.FullName, Password:=_
"abc", AccessMode:=xlShared
End If
Application.DisplayAlerts = True

De todas formas, muchas gracias por tu ayuda. Si se te ocurre algo mas me comentas yo seguiré intentándolo..

Gracias.

Cuando usas esas opción de "proteger y compartir libro" las cosas cambian un "poco":

Application.DisplayAlerts = False
'evaluamos si el libro esta compartido
If ActiveWorkbook.MultiUserEditing Then
'si está compartido le quitamos la protección
ActiveWorkbook.UnprotectSharing "abc"
'Nos aseguramos 
MsgBox "sin compartir"
'guardamos el libro nuevamente como compartido
ActiveWorkbook.ProtectSharing Password:="abc", SharingPassword:="abc"
End If
'activamos alertas
Application.DisplayAlerts = True

Comentas

Abraham Valencia

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas