98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft VBA Training and help » vba courses london - Referencing Worksheets in a formula using R1C1 | VBA forum
vba courses london - Referencing Worksheets in a formula using R1C1 | VBA forum
Resolved · Low Priority · Version Standard
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
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
VBA tip:Stuck in a Code LoopIf you ever get stuck in an infinite code loop when programing in VBA use |