Macro syntax

alainfranco

New Member
Joined
Mar 7, 2012
Messages
38
Hello,

Trying to replace a cell that could be emptied by a user by a formula.

Is am using this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("O24").Value = "" Then
Range("O24").Formula = "=IF(G3<>"Completed", "Project not Completed", "Enter Forecasted Budget at Completion")"


End If
End Sub



As you can imagine, its causing an error on the Range("O24").Formula = ....

Any idea how I can get this to work?

Thanks,
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("O24").Value = "" Then
Range("O24").Formula = "=IF(G3<>""Completed"", ""Project not Completed"", ""Enter Forecasted Budget at Completion"")"
End If
End Sub

You can do this, but it will still allow your cell to be changed. If a user changes it to anything other than nothing, it will remain whatever they enter and your formula will be gone.
 
Upvote 0
A sheet change event script will only be activated when a manual change is made to a cell value.
Not when a formula causes a cell value to change.
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("O24").Value = "" Then
Range("O24").Formula = "=IF(G3<>""Completed"", ""Project not Completed"", ""Enter Forecasted Budget at Completion"")"
End If
End Sub

You can do this, but it will still allow your cell to be changed. If a user changes it to anything other than nothing, it will remain whatever they enter and your formula will be gone.


Hi,

Thanks, yup, that did it.... Its the double quotes I was forgetting!

Thanks, much appreciated! :)
 
Upvote 0
You also don't need the End If as you are only testing one condition so it can all go on the one line.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("O24").Value = "" Then Range("O24").Formula = "=IF(G3<>""Completed"", ""Project not Completed"", ""Enter Forecasted Budget at Completion"")"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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