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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Worksheet_Calculate (note spelling!) doesn't take a Target argument. What range of cells do you want to check?
 
Upvote 0

lenze

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

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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

Spartan300

Board Regular
Joined
Jul 1, 2008
Messages
71
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

lenze

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

Spartan300

Board Regular
Joined
Jul 1, 2008
Messages
71
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,191,587
Messages
5,987,512
Members
440,098
Latest member
MickyMouse123

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