worksheet_calculate to count times specific cell calculated

tp_ryhansen

New Member
Joined
Dec 5, 2005
Messages
1
I am trying to get my excel spreadsheet to count the number of times another cell changes as a result of a calculation on another worksheet in my spreadsheet. Make sense? So I have it so that it currently counts successfully but it is counting for any calculation made and I want the counter to go up only when a specific cell is calculated.

Here is the formula I have so far:

Code:
Private Sub Worksheet_Calculate()
With Me.Range("B2:B3")
If Me.Range("B2") Then
Me.Range("G2").Value = ((Me.Range("G2").Value) + 1)
ElseIf Me.Range("B3") Then
Me.Range("G3").Value = ((Me.Range("g3").Value) + 1)
Else
End If
End With
End Sub

So I want the value of G2 to go up only when B2 changes as the result of calculation. And I want G3 to go up only when B3 changes as the result of a calculation. With the code as it is now G2 goes up when B2 AND B3 change. G3 won't go up by one at all. What is wrong?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You're using an ElseIf statement, which means only one of those if statements will run at any time--not both.

Just use 2 separate statements. This seems to work for me:

Code:
Private Sub Worksheet_Calculate()

If Range("B2") Then Range("G2").Value = Range("G2").Value + 1
If Range("B3") Then Range("G3").Value = Range("G3").Value + 1

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,525
Members
449,088
Latest member
RandomExceller01

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