Formula Works In Worksheet, NOT In Macro

clarkerots

New Member
Joined
Mar 29, 2019
Messages
10
I'm using a INDEX/MATCH formula to pull data over from one Excel spreadsheet to another. My goal is that when data ends up as an #NA or #REF , the cell stays blank. I'm using IFERROR for that part. When I enter the formula in the cell, it works fine. When I enter the formula in a macro and run, I get an object error.

The formula in the worksheet is:
=IFERROR(INDEX('[Basketball Scoresheet-Gameday.xlsm]Sheet1'!$AZ$3:$AZ$14,MATCH(A2,'[Basketball Scoresheet-Gameday.xlsm]Sheet1'!$C$3:$C$14,0)),"")

That takes my data in A2, matches it, and displays the data I'm looking to pull. Also, the range on my final worksheet is C2:C22.

In my Macro, called Test, the formula is the following:
Sub Test()

Range("C2:C22").Formula = "=IFERROR(INDEX('[Basketball Scoresheet-Gameday.xlsm]Sheet1'!$AZ$3:$AZ$14,MATCH(A2,'[Basketball Scoresheet-Gameday.xlsm]Sheet1'!$C$3:$C$14,0)),"")"

End Sub

When I run the Macro I get a Run-Time error '1004': Application-defined or object-defined error.

Why would a formula run on a worksheet, and the same exact formula give me an error in a macro?

Any help appreciated. Thank you.
 

Some videos you may like

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,872
Office Version
365
Platform
Windows
You need to double-up on the quotes inside the formula like
Code:
Range("C2:C22").Formula = "=IFERROR(INDEX('[Basketball  Scoresheet-Gameday.xlsm]Sheet1'!$AZ$3:$AZ$14,MATCH(A2,'[Basketball  Scoresheet-Gameday.xlsm]Sheet1'!$C$3:$C$14,0)),"[COLOR=#ff0000]""[/COLOR]")"
 

Watch MrExcel Video

Forum statistics

Threads
1,095,971
Messages
5,447,604
Members
405,458
Latest member
newbie111

This Week's Hot Topics

Top