Populate columns with formula automatically

deduwa

Board Regular
Joined
Jul 28, 2015
Messages
110
I am looking for a code that does the following in a data set with headings & with a dynamic number of rows (Using existing data in column B to indicate where the last row is);

--- Cell range J2 to last row in column J to be populated with the formula =G2-I2 in row 2, =G3-I3 in row 3 all the way to the last row

--- Cell range M2 to last row in column M to be populated with the formula =if(K2="",G2,K2) in row 2, =if(K3="",G3,K3) in row 3 all the way to the last row

--- Cell range O2 to last row in column O to be populated with the formula =M2-I2 in row 2, =M3-I3 in row 3 all the way to the last row

--- Cell range P2 to last row in column P to be populated with the formula =SUM(E2:F2) in row 2, =SUM(E3:F3) in row 3 all the way to the last row

--- Cell range Q2 to last row in column Q to be populated with the formula =E2*O2/100 in row 2, =E3*O3/100 in row 3 all the way to the last row

--- Cell range R2 to last row in column R to be populated with the formula =F2*O2/100 in row 2, =F3*O3/100 in row 3 all the way to the last row

--- Cell range S2 to last row in column S to be populated with the formula =Q2+R2 in row 2, =Q3+R3 in row 3 all the way to the last row


Thank you
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Normally when using code we would enter the result of the formula into the cell not the formula.
If you want the formula why not enter the formula manually and then drag down
 
Upvote 0
Normally when using code we would enter the result of the formula into the cell not the formula.
Not necessarily. If the data can change, they may want it to be dynamic.

deduwa,
If you turn on the Macro Recorder, and record yourself entering all those formulas in row 2, you will get all the formulas you need.
Then, it is just a matter of changing the range on the left side of the formula to do all the rows instead of that one row, i.e.

So, if you recorded the first one, it will show you:
Code:
    Range("J2").Select
    ActiveCell.FormulaR1C1 = "=RC[-3]-RC[-1]"
You can combine these two rows to this:
Code:
    Range("J2").FormulaR1C1 = "=RC[-3]-RC[-1]"
Now, we can find the last row like this:
Code:
Dim lastRow as Long
lastRow = Cells(Rows.Count,"B").End(xlUp).Row
So, now just change your range in your formula like this:
Code:
    Range("J2:J" & lastRow).FormulaR1C1 = "=RC[-3]-RC[-1]"

So, your code would start like this:
Code:
Dim lastRow as Long
lastRow = Cells(Rows.Count,"B").End(xlUp).Row

Range("J2:J" & lastRow).FormulaR1C1 = "=RC[-3]-RC[-1]"
and then just continue on adding the other formulas underneath it.
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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