VBA to selectt a variable range of cells to protect

glynn1969

Board Regular
Joined
Nov 24, 2018
Messages
80
Office Version
  1. 365
Platform
  1. Windows
Hello

I am looking for a vba code to allow a user to enter the value for the range of cells to protect on a worksheet.

Ideally i would like it that when i run the macro then a message box would pop up to ask the user to enter the cell range values then to confirm and the cell range to be protected.

EG the mesage would ask for the cells - i would enter b2 and d9 the the range B2:D9 would be protected with the remaining worksheet not protected.

Hope someone can help.

Thank you
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Give this code a try (it allows you to type the range in, for example, B2:D9, or to select the range on the sheet by left-click dragging the mouse)...
Code:
[table="width: 500"]
[tr]
	[td]Sub ProtectRange()
  Dim Rng As Range
  ActiveSheet.Unprotect
  Set Rng = Application.InputBox("Type or select the range to protect:", Type:=8)
  Cells.Locked = False
  Rng.Locked = True
  ActiveSheet.Protect
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
That works fantastic.
Just one more thing is is possible for the inpu to be made in two separate entries
eg first enter start of range, such as B2.

Then a second box for the end of the range, such as D9.

No problem if this is difficult as you have solved my issue with a lovely macro.

Thank you
 
Upvote 0
Is it posible to apply the same range across two or more worksheets at once with this macro.
 
Upvote 0
That works fantastic.
Just one more thing is is possible for the input to be made in two separate entries
eg first enter start of range, such as B2.

Then a second box for the end of the range, such as D9.
Sure, no problem. I did not give you this at first because I thought it more cumbersome for the user (you have to type or select a cells, then say OK, then do it again for the second cell)... I figured since you knew both cells, why not enter or select everything at once. But if you want the two step procedure, here is the code for it (note the name change... you can change it back if you want)...
Code:
[table="width: 500"]
[tr]
	[td]Sub ProtectRange2()
  Dim TL As Range, BR As Range
  ActiveSheet.Unprotect
  Set TL = Application.InputBox("Enter or select the top left cell of range to protect:", Type:=8)
  Set BR = Application.InputBox("Enter or select the bottom right cell of range to protect:", Type:=8)
  Cells.Locked = False
  Range(TL, BR).Locked = True
  ActiveSheet.Protect
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Thank you very much for this.
I would post a like/thank you to your profile but cant see how to...will keep searching
 
Upvote 0
I would post a like/thank you to your profile but cant see how to...will keep searching
Look at the bottom right of each post & you will see "Thank you for posting this" & "Like this post"
You can click on them to show your appreciation.
 
Upvote 0
:oops: I can't even tell my left from my right!!
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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