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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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
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
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,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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