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
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi,

Right Click on the sheet Tab in question, paste this in;

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, [B]Range("B2")[/B]) Is Nothing Then
    MyMacro
End If

End SUb

Chnage the Range("B2") to meet your specified range anf the My Macro part for your desired actions.
 
Last edited:
Upvote 0
Hi,

Actually change that too;

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If HasFormula(Target) Then
    MyMacro
End If

End Sub
 
Upvote 0
Question where it says my macro do I insert the macro name or the entire macro
 
Upvote 0
Could be either way, depending on what your macro does. Let's back up a minute. What do you want to do?
lenze
 
Upvote 0
I would like to have a macro that is currently set up, to run if a change is made to any cell that has a formula in it. Basically putting the formula right back in the cell. Locking the cells is causing other issues so I thought if there were a few macros in place then it would not matter if a user accidentally over wrote a formula.

Thanks
Mark
 
Upvote 0
OK, Then my second event should work fine; either place the body of your other macro where the MyMacro is or simply type the name of the other macro to call it.
 
Upvote 0
No need for a seperate macro
Code:
Private Sub WorkSheet_Change(byVal Target as Range)
If Target.hasFormula Then
     Applications. EnableEvents = False
     MsgBox. "You may not change this cell"
     Application.Undo
     Application.EnableEvents = True
End If
End Sub
untested

lenze
 
Upvote 0
Private Sub Worksheet_Change(ByVal Target As Range)
If HasFormula(Target) Then
Sub UpdatingFormulas()
End If
End Sub

When I run this macro I get a compile error "Sub or Function not defined"

It highlights "hasformula" with a blue bar

When I click on ok "Private Sub Worksheet_Change(ByVal Target As Range)" is highlighted in yellow.

Is there something I am doing wrong

Thanks

Mark
 
Upvote 0
Private Sub WorkSheet_Change(ByVal Target As Range)
If Target.HasFormula Then
Applications.EnableEvents = False
MsgBox. "You may not change this cell"
Application.Undo
Application.EnableEvents = True
End If
End Sub

When I use this macro I get a Compile Error "Syntax Error"
MsgBox. "You may not change this cell" - this is highlighted in blue
Private Sub WorkSheet_Change(ByVal Target As Range) - this is highlighted in yellow

Do I need to make any adjustments

Thanks
Mark
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,585
Members
448,972
Latest member
Shantanu2024

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