Shundra9
New Member
- Joined
- Oct 18, 2013
- Messages
- 10
- Office Version
- 2021
- 2019
- 2016
- 2013
- 2011
- 2010
- 2007
Long story short, I am trying to add a defined name to the workbook by running the following Excel VBA code:
When this code runs, it throws the following error message:
Run-time error '1004':
The formula you typed contains an error.
Which of course is the same error I would get if I tried to manually add the name and it had an incorrect formula. However, if I do try to manually define the name as the code is running (by running the first line in the Immediate Window then copy-pasting the result into the "New Name" dialog), everything works fine. I'm very confused. Why is this VBA macro giving me an error that doesn't occur outside of the macro?
Thanks in advance for your help.
Code:
reference = "=OFFSET(Recording_" & numFiles & "!$A$1:$A$" & maxRows & ",0," & MEA_DIMENSION & "*(ROW(Averages!B2)-2)+(COLUMN(Averages!B2)-2))"
ActiveWorkbook.Names.Add name:="dataColumn" & numFiles, RefersToR1C1:=reference
When this code runs, it throws the following error message:
Run-time error '1004':
The formula you typed contains an error.
- For information about fixing common formula problems, click Help.
- To get assistance in entering a function, click Function Wizard (Formulas tab, Function Library group).
- If you are not trying to enter a formula, avoid using an equal sign (=) or minus sign (-), or preceed it with a single quotation mark (').
Which of course is the same error I would get if I tried to manually add the name and it had an incorrect formula. However, if I do try to manually define the name as the code is running (by running the first line in the Immediate Window then copy-pasting the result into the "New Name" dialog), everything works fine. I'm very confused. Why is this VBA macro giving me an error that doesn't occur outside of the macro?
Thanks in advance for your help.