Solved

# Excel VBA Loop to add Formula

Posted on 2012-09-18
458 Views
I am trying to create a simple loop that inserts a formula but I seem to be missing something. I want to:
1) Find the last row with data in it
2) Starting in cell T3 and looping all the way down to the last row I want to insert a formula
3) The formula should be the sum of colums F through S.
What am I missing?

Sub GetTotal()
Dim lstrow As Integer
Dim i As Integer

Application.ScreenUpdating = False
lstrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = lstrow
Range("T3" & lstrow).Formula = "=SUM(RC[-14]:RC[-1])"
Next i
Application.ScreenUpdating = True

End Sub

0
Question by:minamina6

LVL 33

Accepted Solution

Try this.
``````Sub GetTotal()
Dim lstrow As Integer
Dim i As Integer

Application.ScreenUpdating = False

lstrow = Cells(Rows.Count, 1).End(xlUp).Row

Range("T3:T" & lstrow).FormulaR1C1 = "=SUM(RC[-14]:RC[-1])"

Application.ScreenUpdating = True

End Sub
``````
0

Author Closing Comment

Thanks imnorie!
0

## Featured Post

### Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …