Use FormulaR1C1 to populate formula if cell contains specified value

CD2017

New Member
Joined
Feb 21, 2017
Messages
33
Hello,

I need help with inserting a multiplication formula that is using two cell values in the formula IF another cell contains a specific value.

If cell G1 = "1", then I want to insert a formula in cell H1 as follows G1*D1, and so on throughout the table of data.

The table of data will change so I don't want to hardcode the cell references in the formula.

Below is the code I have so far, but I'm missing a step. The code currently sees the value "1" in cells G8, G9, G10 and is populating the formula into every cell in Column H within the workbook. This is not what I want.
I want the formula to populate in cells H8, G9, H10 since the value is "1" in the corresponding G column.

Any help would be greatly appreciated!

Thanks

Sub InsertFormula()
Dim cell As Range

For Each cell In Sheets("InvestmentRevalPS").Range("G:G")

If cell.Value = "1" Then
Sheets("InvestmentRevalPS").Range("H:H").FormulaR1C1 = "=R[-0]C[-1]*R[-0]C[-4]"

End If

Next

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hello,

I need help with inserting a multiplication formula that is using two cell values in the formula IF another cell contains a specific value.

If cell G1 = "1", then I want to insert a formula in cell H1 as follows G1*D1, and so on throughout the table of data.

The table of data will change so I don't want to hardcode the cell references in the formula.

Below is the code I have so far, but I'm missing a step. The code currently sees the value "1" in cells G8, G9, G10 and is populating the formula into every cell in Column H within the workbook. This is not what I want.
I want the formula to populate in cells H8, G9, H10 since the value is "1" in the corresponding G column.

Any help would be greatly appreciated!

Thanks

Sub InsertFormula()
Dim cell As Range

For Each cell In Sheets("InvestmentRevalPS").Range("G:G")

If cell.Value = "1" Then
Sheets("InvestmentRevalPS").Range("H:H").FormulaR1C1 = "=R[-0]C[-1]*R[-0]C[-4]"

End If

Next

End Sub
this will fix the formula in all cells issue

Code:
Sub InsertFormula()
 Dim cell As Range

 For Each cell In Sheets("InvestmentRevalPS").Range("G:G")

 If cell.Value = "1" Then
 Sheets("InvestmentRevalPS").Range("H" & cell.row).FormulaR1C1 = "=R[-0]C[-1]*R[-0]C[-4]"

 End If

 Next

 End Sub
 
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,304
Members
448,886
Latest member
GBCTeacher

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