Having a macro run if changes are made to certain cells

Mldeuser

Well-known Member
Joined
Dec 27, 2008
Messages
573
Office Version
  1. 365
Platform
  1. Windows
Is it possible to write a macro that will run if a user enters data into any cell in the worksheet that has a formula in it.

Thanks
Mark
 
Sorry, my code will not work as written. Try this for the time being
Code:
Private Sub WorkSheet_Change(ByVal Target As Range)
If Not Target.HasFormula Then
     Application.EnableEvents = False
     MsgBox "You may not change this cell"
     Application.Undo
     Application.EnableEvents = True
End If
End Sub
This will keep them from overwriting with a constant, but not a formula. It's a start

Be sure and remove the . from the MsgBox line
lenze
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
This one works, but it keeps the user fom entering data into the non formula cells.

Thanks
Mark
 
Upvote 0
OK: Let's step back and simplify!!
Choose Edit>GoTo>Special>Formulas>OK
This will select all the cells with formulas. Now, with them selected, assign them a Name. Insert>Name>Define (say myFormulas) Now the code
Code:
Private Sub WorkSheet_Change(ByVal Target As Range)
If Not Intersect(Target,Range("myFormulas")) Is Nothing Then 
     Application.EnableEvents = False
     MsgBox "You may not change this cell"
     Application.Undo
     Application.EnableEvents = True
End If
End Sub

lenze
 
Upvote 0
Thanks this now only affects the cells with formulas.

One other question, can it be modified to allow for rows to be inserted or deleted. This is one of the reasons that locking the cells is not a viable option.

Thanks
Mark
 
Upvote 0
Is it possible to allow users to copy one row and insert in in the spreadsheet keeping the protection in place.
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,090
Members
449,065
Latest member
Danger_SF

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