ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Formula in a range which varies in size (https://www.excelbanter.com/excel-programming/349805-formula-range-varies-size.html)

Alan M

Formula in a range which varies in size
 

Hi I need code to be able to scroll through the cells in a range of variable
size and add the values in the in columns N and O and place the result in
column Q for each row in the range. The number of rows varies each time.

Thanks

Dave Peterson

Formula in a range which varies in size
 
How do you determine the range of rows?

Option Explicit
Sub testme()

Dim FirstRow As Long
Dim LastRow As Long

With Worksheets("sheet1")
FirstRow = 2 'headers in row 1 and row 2 starts the data
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

With .Range(.Cells(FirstRow, "Q"), .Cells(LastRow, "Q"))
.FormulaR1C1 = "=SUM(RC[-3]:RC[-2])"
'.Value = .Value
End With
End With

End Sub

This actually puts a formula in that range. If you want the value, then
uncomment that ".value = .value" line.

And I used column A to determine the last row to populate.

Alan M wrote:

Hi I need code to be able to scroll through the cells in a range of variable
size and add the values in the in columns N and O and place the result in
column Q for each row in the range. The number of rows varies each time.

Thanks


--

Dave Peterson


All times are GMT +1. The time now is 05:41 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com