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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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