Creating formula with VBA

gt93grad

Board Regular
Joined
Dec 7, 2011
Messages
54
I need to apply this formula via VBA code, but I'm not having much luck. This is the formula: =IF(ISERROR(VLOOKUP(D7,SparesOriginsLookup,4,FALSE)),"HKN1",VLOOKUP(D7,SparesOriginsLookup,4,FALSE))

I'm fine with using D7, or RC[-1], but I can't get the quotes right when apply within the code. If the VLOOKUP works, apply the value. If not, set HKN1. Can someone help here?

I see examples online, but this is tough to recreate.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
It would be much easier if you posted the code that doesn't work, but lacking that, double up the quotes.
 
Upvote 0
It would be much easier if you posted the code that doesn't work, but lacking that, double up the quotes.

Here's what I have. The first section is attempting to use RC, and second is attempting to use D7.

ActiveCell.FormulaR1C1 = "=""IF(ISERROR(VLOOKUP("" & RC[-1] & "",SparesOriginsLookup,4,FALSE)),""HKN1"",VLOOKUP("" & D7 & "",SparesOriginsLookup,4,FALSE))"
 
Upvote 0
You could use the macro recorder to record entering the formula into a cell, that would give you properly syntaxed VB code.
 
Upvote 0
Oh yeah. Forgot the easiest way. That works great. Thanks. Here's the answer for others:

ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-1],SparesOriginsLookup,4,FALSE)),""HKN1"",VLOOKUP(RC[-1],SparesOriginsLookup,4,FALSE))"
 
Upvote 0
Oh yeah. Forgot the easiest way. That works great. Thanks. Here's the answer for others:

ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-1],SparesOriginsLookup,4,FALSE)),""HKN1"",VLOOKUP(RC[-1],SparesOriginsLookup,4,FALSE))"


Spoke too soon. Now it's putting the formula in the cell as a literal, and not resolving it. I changed the type to General, clicked in the field, and hit enter again, but no luck.
 
Upvote 0
Nope, never mind. It's still inserting as a literal. Let me know if you can see anything wrong.

ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-1],SparesOriginsLookup,4,FALSE)),""HKN1"",VLOOKUP(RC[-1],SparesOriginsLookup,4,FALSE))"
 
Upvote 0
Does it reference a cell formatted as Text? If so, change the format of the cell containing the formula to something other than General, e.g., Number.

Also, you might simplify to

... = "=IFERROR(VLOOKUP(RC[-1], SparesOriginsLookup, 4, FALSE), ""HKN1"")"
 
Upvote 0
Here's a similar problem, but I can't get this one to work either. "StageCostRange" is a variable. I can't figure out where the quotations and apostrophes should go.

ActiveCell.FormulaR1C1 = "=IFERROR('VLOOKUP(A7&B7, " & StageCostRange & " ,2,FALSE)',0)"
 
Upvote 0

Forum statistics

Threads
1,203,558
Messages
6,056,078
Members
444,844
Latest member
Taps07

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