Can't Add a Valid Defined Name in Excel VBA

Shundra9

New Member
Joined
Oct 18, 2013
Messages
10
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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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