Trying to insert formula into one cell if another cell contains certain text

davcurnutt

New Member
Joined
Mar 31, 2023
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I am trying to us VBA to input a formula in column O if column A contains the text "Cancel Details". If it contains the text, I want a formula inserted in Column O that will multiply column I and J and add column M. There are multiple worksheets in the workbook and I want it to do this to all sheets. This is what I put in VBA but doesn't work.

Range("O1:O900").Formula = _
"=IF(($A1="Cancel Details",=($I1*$J1)+$M1))"
 
Sub CancelFormula()
'
' CancelFormula Macro
'

'
Range("O1:O900").FormulaR1C1 = "=IF($A1=""Cancel Details:"",(($I1*$J1)+$M1),("" ""))"
End Sub
yeah, sorry about that, i had made a mistake when i first posted it, but then changed it (included r1c1)
try this
Range("O1:O900").Formula = "=IF($A1=""Cancel Details"",($I1*$J1)+$M1)"
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
yeah, sorry about that, i had made a mistake when i first posted it, but then changed it (included r1c1)
try this
Range("O1:O900").Formula = "=IF($A1=""Cancel Details"",($I1*$J1)+$M1)"
That worked. Any tips on how I can get it to leave the cell blank if it doesn't meet that criteria. Also if I want it to run through the whole workbook do I add that to the range?
 
Upvote 0
VBA Code:
      Sub CancelFormula()
         Dim ws As Worksheet
         For Each ws In Worksheets
        ws.Range("O1:O900").Formula = "=IF($A1=""Cancel Details"",(($I1*$J1)+$M1),("" ""))"
         Next
      End Sub
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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