Run code on formula change

Spartan300

Board Regular
Joined
Jul 1, 2008
Messages
71
Hi,

Hope someone can help. I have the following code...

Code:
Private Sub Worksheet_Calcualte(ByVal Target As Range)
Dim Cell As Range
    If Target.Column <> 3 Then Exit Sub
    Application.EnableEvents = False
    For Each Cell In Target.Cells
        With Cell
            If .Value = "Yes" Then
                .Offset(, 2).FormulaR1C1 = Date
            Else
                .Offset(, 2).ClearContents
            End If
        End With
    Next Cell
    Application.EnableEvents = True
End Sub


Code:
If .Value = "Yes" Then

relates to the formula =IF('Field Sales Manager'!$R$201>Commission!B14,"Yes","No"). The problem I have is that because its a formula, its not recognising the "Yes" and the code will not run.

So my question is, using the formula can I get the code to run?

Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Worksheet_Calculate (note spelling!) doesn't take a Target argument. What range of cells do you want to check?
 
Upvote 0
The Calculate Event (check your spelling) does not set a Target range. So,
Code:
Private Sub Worksheet_Calculate()
Dim cl as Range
Application.EnableEvents = False
For Each cl in Range("$C$2:$C" & Range("$C$65536").End(xlUp).Row)
    If cl ="Yes" Then
        Cells(Cl.Row,5) = Date
    Else: Cells(cl.Row,5).ClearContents
    End If
Next cl
Application.EnableEvents = True
End Sub
What are you really trying to do? There might be a better way.
lenze
 
Upvote 0
It would be something like

Code:
Private Sub Worksheet_Calculate()
Dim Cell As Range
    Application.EnableEvents = False
    For Each Cell In Range("C1:C100")
        With Cell
            If .Value = "Yes" Then
                .Offset(, 2).Value = Date
            Else
                .Offset(, 2).ClearContents
            End If
        End With
    Next Cell
    Application.EnableEvents = True
End Sub
 
Upvote 0
Thanks for the reply.

The target range is the whole of Column C I want the date to be entered into column E if Column C formula = Yes.

Thanks
 
Upvote 0
OK. Both codes will do that. For grins, however, how are 'Field Sales Manager'!$R$201 and Commission!B14 cHanged? You might be able to use a WorkSheet_Change Event.
lenze
 
Upvote 0
Thanks very much for your suggestions and help.

I tried this yesterday and it wouldn't work? but now it is perfect.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,382
Messages
6,119,194
Members
448,874
Latest member
Lancelots

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