Run macro on change in value

shaun111

New Member
Joined
May 12, 2008
Messages
30
Hi,

I am struggling with what I know is a very simple piece of code. I was hoping someone could please help?

I would like excel to run a particular Macro (simple copy past value), if the value (formula) in cell "c3" on sheet "assumptions" is anything other than 0.

I am using the following in the code of the "assumptions" sheet which is not working.

rivate Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count = False Then Exit Sub
If Not Intersect(Target, Range("C4")) Is Nothing Then
Sheets("Assumptions").Select
Range("C202").Select
Selection.Copy
Range("C201").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

End If


Any suggestions?

Thanks
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

TKB

Board Regular
Joined
Aug 18, 2011
Messages
128
Hello,

Not knowing what your trying to achieve other than the copy and paste, you could try the following.

In VBA select the Assumptions page to write a private Sub (macro)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Range("A1").Value = Range("A2").Value Then


Exit Sub


Else


Range("A2").Value = Range("A1").Value


End If


End Sub
 

shaun111

New Member
Joined
May 12, 2008
Messages
30
Hi,

I'm not sure exactly if that will help:

I need to the code to automatically rund the macro 'DSRC' every time cell 'B3' in the assumptions sheet is greater or less than 0?
 

TKB

Board Regular
Joined
Aug 18, 2011
Messages
128
Try this:

Code:
Private Sub Worksheet_Change(byval target as range)

IF Range("B3").Value > 0 or Range("B3").Value < 0 then

'Insert DSRC Macro Here

Else
End If

End Sub
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,763
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows

ADVERTISEMENT

Or
Code:
Private Sub Worksheet_Change(ByVal target As Range)
    If Range("B3").Value <> 0 Then Call DSRC
End Sub
 

shaun111

New Member
Joined
May 12, 2008
Messages
30
Thanks for the help all, realised that what I was looking for was

Sub Worksheet_Calculate () and not Sub Worksheet_change as I wanted it to calc automatically once the target cell changed, the target cell is the result of a formula.

Ended up using

Private Sub Worksheet_Calculate()

If Sheets("Assumptions").Range("C3").Value > 0 Then
Application.ScreenUpdating = False
DSCR
End If

End Sub

Which seems to work quite well.

Thanks for the help
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,763
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Pleasure...I'd probably move the screenupdating to the beginning though
Code:
Private Sub Worksheet_Calculate()
Application.ScreenUpdating = False
If Sheets("Assumptions").Range("C3").Value > 0 Then DSCR 
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,079
Messages
5,599,636
Members
414,326
Latest member
kfg1287

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