Reset cell value to 0 everytime value reaches 4

nt82

Board Regular
Joined
Jan 29, 2009
Messages
133
Hello,

I've a cell C1 which has a value through formulas ( A1+ B1). Since everytime value in A1 or B1 changes then value in C1 changes as well.

Now, I need a macro which should automatically reset cell A1 and B1 value to "1" whenever value in cell is "5"

Hope this makes sense. thanks in advance

NT
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Paste this in the sheet module
Code:
Sub worksheet_selectionchange(ByVal target As Range)
Set target = Range("C1")
    If target.Value > 4 Then
        Range("A1:B1").Value = 1
    End If
End Sub
 
Upvote 0
That may be sufficient for your needs but there are circumstances where that code will fail because it uses the Selection_Change event and not the Calculate event.

For example try these actions.

1. Select A1:B1, type 6, press Ctrl+Enter. (Result is 12 in C1 and A1:B1 not reset.)

2. Clear A1:B1 then enter 2 in A1, select B1, type 6 and confirm by clicking the 'tick' just to the left of the formula bar. (Result is 8 in C1 and A1:B1 not reset)

3. Type 7 in G1 & H1, select G1:H1, Copy, select A1:B1, Paste. (Result is 14 in C1 and A1:B1 not reset).

I think this would be more robust ..
Code:
Private Sub Worksheet_Calculate()
    If Range("C1").Value > 4 Then
        Range("A1:B1").Value = 1
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,550
Members
452,927
Latest member
rows and columns

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