Martin has attended:
Excel VBA Intro Intermediate course
Referencing Worksheets in a formula using R1C1
I am trying to insert a formula using a loop that refers to a number of worksheets, but cannot get a variable to work using FormulaR1C1. The code is as follows:
Sub SummarysheetSalesVariances()
Dim LastRow As Integer
Dim StartSheet As Integer
Dim NextCell As Integer
LastRow = Range("a65536").End(xlUp).Row
StartSheet = 6
NextCell = 9
Do While StartSheet < LastRow + 1
Sheets("Summary").Select
Range("b" & NextCell).Select
ActiveCell.FormulaR1C1 = "=worksheets(6)!r[46]c"
StartSheet = StartSheet + 1
NextCell = NextCell + 1
Loop
End Sub
any help appreciated.
Thanks
Martin
RE: Referencing Worksheets in a formula using R1C1
R1C1 doesn't accept the use of Letters for columns
ActiveCell.FormulaR1C1 = only works by setting the range that counts the diffrence between the top and bottom of the range
eg ActiveCell.FormulaR1C1 = "=SUM(R[-42]C:R[-1]C)"
which means add up the range from the cell 42 rows away to the cell just above
When adding up a column of numbers whose length keeps changing it is always better to:
Create a variable
Select the respective sheet
Create a loop that goes down the list adding each new number to the variable
When the loop is completed move down to the next empty cell and paste the value in the variable