Autofill formula based on another column (VBA)

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello....

Does anyone have any idea how to fix the autofill formula below?
Currently I'm using this code but then it recognises B30 as the last row.
Is it possible to make it to autofill only until B20 instead?
The list is dynamic, can be shorter or longer so I can't set it to B3:B20.

Thank you in advance.


VBA Code:
 Range("F3").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-2]"
    Range("F3").Select
    Selection.AutoFill Destination:=Range("F3:F" & Range("B" & Rows.Count).End(xlUp).Row)
    Columns("F:F").Select
    Selection.Style = "Percent"


Book1
ABCDEF
1Data
2PICTotal sales qtyTotal sales amountProfitProfit (%)
3Total187,145473,302,637105,049,99822%
4A10,48458,263,61512,696,86522%
5B13,45546,572,5617,317,80816%
6C7,46741,699,4708,462,40020%
7D7,41636,529,9236,720,20118%
8E6,92836,495,2838,205,26222%
9F33,12536,168,12913,561,27737%
10G6,10432,617,6167,784,11824%
11H5,84032,315,1236,290,06519%
12I5,07828,652,5946,546,68123%
13J4,95625,176,0565,717,27023%
14K4,39323,531,0875,226,97922%
15L3,55219,407,7474,082,57421%
16M3,60119,205,2164,275,47522%
17N3,48618,915,5904,184,61422%
18O2,70014,638,4423,261,90322%
19P68,5142,806,565620,32222%
20Q46307,62096,18431%
21#DIV/0!
22#DIV/0!
23#DIV/0!
24#DIV/0!
25#DIV/0!
26PICTotal sales qtyTotal sales amountProfit#VALUE!
27This yearSQ39,61739,265,85516,734,74943%
28Last yearSQ33,12536,168,12913,561,27737%
29#DIV/0!
30NowDiff-6,492-3,097,726-3,173,472102%
Sheet1
Cell Formulas
RangeFormula
F3:F30F3=E3/D3
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You could change your formula to : =IF(D3="","",E3/D3)
 
Upvote 0
You could change your formula to : =IF(D3="","",E3/D3)
That's true.... I also thought of that before but there's title in the middle and other formula too at the bottom.
So if I thought if anyone knew the solution to that formula, it'd be best.
If not, then I just do what you suggested and change the other part.
Thank you.
 
Upvote 0
That's true.... I also thought of that before but there's title in the middle and other formula too at the bottom.
So if I thought if anyone knew the solution to that formula, it'd be best.
If not, then I just do what you suggested and change the other part.
Thank you.
If the rows at the bottom always number ten, then :
VBA Code:
 Selection.AutoFill Destination:=Range("F3:F" & Range("B" & Rows.Count).End(xlUp).Row-10)
If the rows do not always number 10 but the bottom 5 rows are always occupied like your sample data, then :
VBA Code:
Selection.AutoFill Destination:=Range("F3:F" & Range("B" & Rows.Count).End(xlUp).Row - 5)
With the second option you would need to change the formula.
 
Upvote 0
Solution
If the rows at the bottom always number ten, then :
VBA Code:
 Selection.AutoFill Destination:=Range("F3:F" & Range("B" & Rows.Count).End(xlUp).Row-10)
If the rows do not always number 10 but the bottom 5 rows are always occupied like your sample data, then :
VBA Code:
Selection.AutoFill Destination:=Range("F3:F" & Range("B" & Rows.Count).End(xlUp).Row - 5)
With the second option you would need to change the formula.
That's perfect. Thank you so much.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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