VBA code to protect cell, (more than one cell?)

jimjaix

Board Regular
Joined
Mar 11, 2009
Messages
58
Hi this code is from Tom (I think)

This code protect A1 cell, I want to know how I can protect let's say A1 to A70 and B50 to B70 without doing this code for every single cell.

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range) 

  If Target.Address <> "$A$1" Then Exit Sub 

   Application.EnableEvents = False 

    MsgBox ("Hey, leave me alone!")

    Application.Undo 

  Application.EnableEvents = True 

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

nbrcrunch

Well-known Member
Joined
Jan 1, 2003
Messages
2,062
Elementary, dear Watson. For example, If you had thought to try recording your multi-selection, you would have discovered that the syntax is:

(Say I wanted to select A1, C4 and F5...)

Range("A1,C4,F5").Select

You can also do Ranges of cells:

Range("A1:A10,C4:C40,F5").Select

Then follow that with a With Selection clause.
 
Upvote 0

jimjaix

Board Regular
Joined
Mar 11, 2009
Messages
58
I know those range selection, but I just don't know how to put them into that code, can you do it? I am not familar with VBA.
 
Upvote 0

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,282
Responding from PM:



Change
If Target.Address <> "$A$1" Then Exit Sub


To
If Intersect(Target, Range("A1:A70,B50:B70")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
 
Upvote 0

Forum statistics

Threads
1,191,317
Messages
5,985,944
Members
439,991
Latest member
NCWalker

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
Top