VBA build formula as per data length column D

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,362
Office Version
  1. 2010
Hello,</SPAN></SPAN>

Column D data vary; it can be 50.000 rows or can be 50 rows</SPAN></SPAN>

In the cells F5, G5 & H5 I got following formulas as per Column d data length </SPAN></SPAN>
F5 =SUM(F6:F42)</SPAN></SPAN>
G5 =SUM(F6:F42)</SPAN></SPAN>
H5 =SUM(F6:F42)</SPAN></SPAN>


Book1
ABCDEFGHIJ
1
2
3
4
52.09100
6A295
7A155
8A127
9A142
10A106
11A130
12A116
13A82
14A70
15A58
16A59
17A75
18A62
19A75
20A59
21A58
22A61
23A60
24A54
25A41
26A26
27A19
28A36
29A38
30A30
31A27
32A30
33A
34A
35A
36A
37A
38A
39A
40A
41A
42A
43
44
Sheet1


My query is it possible that the formula F5, G5, H5 could be modified by VBA as per data length Column D</SPAN></SPAN>

For example if column D data last row are 20.000, so the formula in F5, G5, H5 can be changed as below by the VBA</SPAN></SPAN>

F5 =SUM(F6:F20000)</SPAN></SPAN>
G5 =SUM(F6:F20000)</SPAN></SPAN>
H5 =SUM(F6:F20000)</SPAN></SPAN>


Thank you all</SPAN></SPAN>

Excel 2000</SPAN></SPAN>
Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Sorry I got mistake current formulas are filled in the cells F5, G5 & H5 are as follow </SPAN></SPAN>
F5 =SUM(F6:F42)
</SPAN></SPAN>
G5 =SUM(G6:G42)
</SPAN></SPAN>
H5 =SUM(H6:H42) as per data length column D last row 42
</SPAN></SPAN>

But when column D filled till last row 20000, I need formula in the cells F5, G5 & H5 could be changed as follow
</SPAN></SPAN>
F5 =SUM(F6:F20000)
</SPAN></SPAN>
G5 =SUM(G6:G20000)
</SPAN></SPAN>
H5 =SUM(H6:H20000)
</SPAN></SPAN>

Regards,
</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Upvote 0
Try
=SUM(OFFSET(F6,,,LOOKUP(2,1/(D:D<>""),ROW(D:D))))
 
Upvote 0
Try
=SUM(OFFSET(F6,,,LOOKUP(2,1/(D:D<>""),ROW(D:D))))
Hello Fluff, formula return #¡NUM! </SPAN></SPAN>

Ok any way I think what I need sum formula which is entered in cell F5 "= SUM(F6:F42)" because column D has data entered till row 42.
</SPAN></SPAN>

So now if I enter data in the Column till rows 20000, I need a macro that can change the formula only in the cell F5
This---> F5 "= SUM(F6:F42)" To This One---> "= SUM(F6:F20000)"
</SPAN></SPAN>

May this help
</SPAN></SPAN>

Thank you

Regards,
</SPAN></SPAN>
Moti
</SPAN></SPAN>
 
Last edited:
Upvote 0
How about
Code:
Sub AddFormula()
   Dim Lr As Long
   Lr = Range("D" & Rows.Count).End(xlUp).Row
   Range("F5").Formula = "=sum(F6:F" & Lr & ")"
End Sub
 
Upvote 0
How about
Code:
Sub AddFormula()
   Dim Lr As Long
   Lr = Range("D" & Rows.Count).End(xlUp).Row
   Range("F5").Formula = "=sum(F6:F" & Lr & ")"
End Sub
Fluff, This is it!! What I needed Perfect Solution</SPAN></SPAN>

Thank you so much for your help
</SPAN></SPAN>

Kind regards,
</SPAN></SPAN>
Moti :)
</SPAN></SPAN>
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,175
Members
449,071
Latest member
cdnMech

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