Write to a sheet if it is protected

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
771
Office Version
  1. 365
Platform
  1. Windows
Afternoon,

I have a spreadsheet that i have had to protect to stop people removing info of it. I also need the spreadsheet to be updated by a macro saving info from a userform to it.

Is there anyway i can proctect it using VB while allowing the userform to submit info to the same sheet.

I hope this makes sense.

Many Thanks
Gavin
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br>    ActiveSheet.Unprotect Password:="Secret"<br>        <SPAN style="color:#007F00">'Write data</SPAN><br>    ActiveSheet.Protect Password:="Secret"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Is there anyway i can proctect it using VB while allowing the userform to submit info to the same sheet.
Yes. Protect the sheet from vba, not manually, like this.
Code:
Sub ProtectUIOnly()
    Sheets("Sheet9").Protect Password:="Secret", UserInterfaceOnly:=True
End Sub
Then your userform code doesn't need to Unprotect/Protect each time. It can just be like this
Code:
Private Sub CommandButton1_Click()
    'Write Data
End Sub
 
Upvote 0
Good Afternoon,

Thanks for the information.
Where would i write the first part.
ie in the workbook initalise or in each sheet that needs protecting.

Cheers
 
Upvote 0
Good Afternoon,

Thanks for the information.
Where would i write the first part.
ie in the workbook initalise or in each sheet that needs protecting.

Cheers
If you are referring to my suggestion then ..

1. Manually Unprotect the sheet.

2. Paste my first code in to a standard module (post back if you need specific directions for how to do that).

3. Edit the code to have the correct sheet name (where I have Sheet9).

4. Back in your workbook, press Alt+F8, select the ProtectUIOnly macro and click run.

Now other code should be able to write to the sheet without unprotecting/reprotecting each time.
 
Last edited:
Upvote 0
Many Thanks,

I will have a look tonight and message you if i have any issue.

Cheers
 
Upvote 0
I just noticed that the last line of my previous post had a typo which changed its meaning greatly. :oops:

I have fixed it now but previously it said
Now other code should be able to write to the sheet with unprotecting/reprotecting each time.
It now says
Now other code should be able to write to the sheet without unprotecting/reprotecting each time.
 
Upvote 0

Forum statistics

Threads
1,224,545
Messages
6,179,432
Members
452,915
Latest member
hannnahheileen

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