Unshare and Reshare a workbook

M

Matt

I have the following to unshare and reshare a workbook. It also adds
and removes worksheet protection based on which command button is
pushed (using two control toolbox - command buttons on worksheet). The
code works the first time I click on one of the buttons but fails on
subsequent clicks. Below is the code. Advice on how to fix this
problem would be appreciated.
Thanks. Matt

Private Sub CommandButton1_Click()

If ThisWorkbook.MultiUserEditing Then
ThisWorkbook.UnprotectSharing
ThisWorkbook.ExclusiveAccess
End If

For Each sh In ThisWorkbook.Worksheets
sh.Unprotect
Next

If Not ThisWorkbook.MultiUserEditing Then
ThisWorkbook.SaveAs Filename:=ThisWorkbook.FullName, _
accessMode:=xlShared
End If

End Sub

Private Sub CommandButton2_Click()

If ThisWorkbook.MultiUserEditing Then
ThisWorkbook.UnprotectSharing
ThisWorkbook.ExclusiveAccess
End If

For Each sh In ThisWorkbook.Worksheets

sh.Protect
Next

If Not ThisWorkbook.MultiUserEditing Then
ThisWorkbook.SaveAs Filename:=ThisWorkbook.FullName, _
accessMode:=xlShared
End If
End Sub
 
M

Matt

I have the following to unshare and reshare a workbook. It also adds
and removes worksheet protection based on which command button is
pushed (using two control toolbox - command buttons on worksheet). The
code works the first time I click on one of the buttons but fails on
subsequent clicks. Below is the code. Advice on how to fix this
problem would be appreciated.
Thanks. Matt

Private Sub CommandButton1_Click()

If ThisWorkbook.MultiUserEditing Then
  ThisWorkbook.UnprotectSharing
    ThisWorkbook.ExclusiveAccess
   End If

For Each sh In ThisWorkbook.Worksheets
sh.Unprotect
Next

If Not ThisWorkbook.MultiUserEditing Then
    ThisWorkbook.SaveAs Filename:=ThisWorkbook.FullName, _
        accessMode:=xlShared
End If

End Sub

Private Sub CommandButton2_Click()

If ThisWorkbook.MultiUserEditing Then
  ThisWorkbook.UnprotectSharing
    ThisWorkbook.ExclusiveAccess
   End If

For Each sh In ThisWorkbook.Worksheets

sh.Protect
Next

If Not ThisWorkbook.MultiUserEditing Then
    ThisWorkbook.SaveAs Filename:=ThisWorkbook.FullName, _
        accessMode:=xlShared
End If
End Sub

Come on this is a pretty good post...and it would really make my day
if somebody took a stab at it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top