I am trying to avoid activating worksheets as I do calculations in my spreadsheet. However, I am running into a problem. This description reproduces the problem exactly on my version of Excel 2016 (32-bit).
Create a new workbook with 2 sheets: Sheet1 and Sheet2. Sheet1 is blank. In Sheet2, in cells A2:A21, I put some integer numbers. I then added a module that uses R1C1 formula format to put some formulas in Column G of Sheet2, with this code:
Option Explicit
Sub Test()
Dim osht As Excel.Worksheet
Dim NumRows As Integer
Set osht = ThisWorkbook.Worksheets("Sheet2")
NumRows = 20
osht.Range("G1").Offset(1, 0).Resize(NumRows, 1).FormulaR1C1 = "=RC[-6]*5"
End Sub
I added a chart sheet to plot what is in Sheet2, with a chart. If you run the code with either Sheet1 or Sheet2 as the active sheet, it will work fine, and the formulas in Sheet2!G2:G21
are correct:
=RC[-6]*5
Run the code with the CHART sheet active, and this does not work. The formulas in Sheet2!G2:G21
look like this:
=R[-1]C[16372]*5
This happens in Excel 2013 (32-bit) as well.
Where am I going wrong here? Or is this an Excel bug?
RC1*5
. – Darren Bartrup-CookFormulaR1C1
, and not toFormula
orFormulaLocal
. I would guess this is a bug. One way around it is toActivate
one of the worksheets before applying the formula, and then re-activate the originally active sheet after applying the formula. (But, obviously, just usingFormula
will be easier.) – YowE3K