Having a macro run if changes are made to certain cells

Mldeuser

Well-known Member
Joined
Dec 27, 2008
Messages
551
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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Mike Blackman

Well-known Member
Joined
Jun 8, 2007
Messages
2,494
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

Mike Blackman

Well-known Member
Joined
Jun 8, 2007
Messages
2,494
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

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
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

Mldeuser

Well-known Member
Joined
Dec 27, 2008
Messages
551
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

Mike Blackman

Well-known Member
Joined
Jun 8, 2007
Messages
2,494
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

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
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

Mldeuser

Well-known Member
Joined
Dec 27, 2008
Messages
551
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

Mldeuser

Well-known Member
Joined
Dec 27, 2008
Messages
551
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,191,483
Messages
5,986,845
Members
440,053
Latest member
jhollingworth

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