Excel/Formula inside a Loop
I am writing a macro which includes the next line:
Range("Sheet2!A1").Formula = "=SUM(Sheet1!A1:Sheet1!E1))"
This runs fine, but if I include the line in a loop it complains
about syntax error and doesn't run.
This is my loop code:
Dim i As Integer
For i = 1 to 5
Range("Sheet2!Ai").Formula = "=SUM(Sheet1!Ai:Sheet1!Ei))"
How can I correct this.
Thank you very much
It's not the loop that's the problem, it's the use of a variable - you have a literal string sheet2!ai - which doesn't make sense to excel. If instead you have
Range("Sheet2!A" & i).Formula = "=SUM(Sheet1!A" & i &":Sheet1!E" & i & "))"
you should find it behaves
(question found in question pool)
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here