VBA: Auto fill formulas down a column

sam82

New Member
Joined
Jan 24, 2011
Messages
7
I have amounts down column D, and quantities of that amount down column C. In column G, I'd like my macro to insert a formula that multiplies the two amounts together for each row to come up with a total.

I was thinking that I could probably create a Do While and Loop macro, but is there an easier way?

Thanks,
 

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.
this is just to show you how it could work. Change what you need. as for the formula, pretend the formula would be EXACTLY what you would put in the first row.
Code:
Sub down_the_columns()
    Range("c1:c50").Formula = "=a1+b1" ' you can make this be whatever.  even the lastrow of A if you prefer.
End Sub
 
Upvote 0
this is just to show you how it could work. Change what you need. as for the formula, pretend the formula would be EXACTLY what you would put in the first row.
Code:
Sub down_the_columns()
    Range("c1:c50").Formula = "=a1+b1" ' you can make this be whatever.  even the lastrow of A if you prefer.
End Sub

Just a quick follow up question, how would I make the macro stop at the last populated row? It doesn't always end at row 200; sometimes it goes to 210 or beyond, sometimes stops at 190.
 
Upvote 0
which column goes the farthest?

Column G, the one that will contain the formulas.

I'd like the macro to stop creating formulas once it reaches that last row that has an amount and quantity in it.

Usually I would just have it go down to say 500, but I saw a method where you can make the macro stop once it reaches the last populated row.
 
Upvote 0
column G is empty then... i'll just assume I should use column A as a reference

Code:
range("G1:G" & cells(rows.count, "A").end(xlup).row).formula = "=a1+b1"
 
Upvote 0
column G is empty then... i'll just assume I should use column A as a reference

Code:
range("G1:G" & cells(rows.count, "A").end(xlup).row).formula = "=a1+b1"

Wow, that would have taken me almost ten lines to do.

It worked just fine, thanks!

I think I understand all that's happening in that code, but I'm not sure what the "A" is for. Could you explain that part?
 
Upvote 0
you'll use a lot of that kind of code
this is used to show the lastrow in column B
Code:
msgbox cells(rows.count, "B").end(xlup).row
and this is to show how many columns are in row 5
Code:
msgbox cells(5, columns.count).end(xltoleft).columns
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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