Can't Add a Valid Defined Name in Excel VBA

Shundra9

New Member
Joined
Oct 18, 2013
Messages
10
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Long story short, I am trying to add a defined name to the workbook by running the following Excel VBA code:

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.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I think your problem is that you're trying to assign the formula as the RefersToR1C1 parameter, but your actual reference uses A1-style addresses. Try instead:

Code:
  ActiveWorkbook.Names.Add Name:="dataColumn" & numFiles, RefersTo:=reference
 
Upvote 0
I think your problem is that you're trying to assign the formula as the RefersToR1C1 parameter, but your actual reference uses A1-style addresses. Try instead:

Code:
  ActiveWorkbook.Names.Add Name:="dataColumn" & numFiles, RefersTo:=reference

Wow, duh! Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,222,441
Messages
6,166,050
Members
452,010
Latest member
triangle3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top