Applying an Excel formula in VBA

SilverBirch

New Member
Joined
Mar 5, 2019
Messages
3
Hello guys,

I need your help.
My created a long formula in Excel and I need to convert it to VBA. I've been trying a couple of things, but always failed. Can you please help me?

So the formula (for example for cell G50) looks like this:
Code:
=IFERROR(INDEX(Lookup!K:K,IFERROR(MATCH(D50,Lookup!D:D,0),IFERROR(MATCH(D50,Lookup!E:E,0),IFERROR(MATCH(D50,Lookup!F:F,0),IFERROR(MATCH(D50,Lookup!G:G,0),MATCH(D50,Lookup!H:H,0)))))),IFERROR(INDEX(Lookup!K:K,IFERROR(MATCH(E50,Lookup!D:D,0),IFERROR(MATCH(E50,Lookup!E:E,0),IFERROR(MATCH(E50,Lookup!F:F,0),IFERROR(MATCH(E50,Lookup!G:G,0),MATCH(E50,Lookup!H:H,0)))))),""))

XaxsOxO.jpg


Lookup is the name of another sheet.
I tried something like this, but with no result

Code:
theCell.FormulaR1C1 = "=IFERROR(INDEX(Lookup!K:K,IFERROR(MATCH(R[0]C[-3],Lookup!D:D,0),IFERROR(MATCH(R[0]C[-3],Lookup!E:E,0),IFERROR(MATCH(R[0]C[-3],Lookup!F:F,0),IFERROR(MATCH(R[0]C[-3],Lookup!G:G,0),MATCH(R[0]C[-3],Lookup!H:H,0)))))),IFERROR(INDEX(Lookup!K:K,IFERROR(MATCH(R[0]C[-2],Lookup!D:D,0),IFERROR(MATCH(R[0]C[-2],Lookup!E:E,0),IFERROR(MATCH(R[0]C[-2],Lookup!F:F,0),IFERROR(MATCH(R[0]C[-2],Lookup!G:G,0),MATCH(R[0]C[-2],Lookup!H:H,0)))))),""))"

Help me, please! :)
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi. Do you know how to use the macro recorder? Record putting the formula into the cell and you will have your code. Your code fails because you are mixing the two reference styles which you cant do. You cant use A1 style, eg K:K, with RC style, eg R[0]C[-3].
 
Upvote 0
Hmm, I've unified the styles, but it doesn't work still.

I get "Application-defined or object defined-error".

Code:
Sub myTest()

Dim sh As Worksheet
Dim i As Integer
Dim c As Range
    
    Set sh = ActiveSheet
    
    For i = 2 To 35
        Set c = sh.Cells(i, 7)
        c.FormulaR1C1 = "=IFERROR(INDEX(Lookup!C[4],IFERROR(MATCH(R[0]C[-3],Lookup!C[-3],0),IFERROR(MATCH(R[0]C[-3],Lookup!C[-2],0),IFERROR(MATCH(R[0]C[-3],Lookup!C[-1],0),IFERROR(MATCH(R[0]C[-3],Lookup!C,0),MATCH(R[0]C[-3],Lookup!C[1],0)))))),IFERROR(INDEX(Lookup!C[4],IFERROR(MATCH(R[0]C[-2],Lookup!C[-3],0),IFERROR(MATCH(R[0]C[-2],Lookup!C[-2],0),IFERROR(MATCH(R[0]C[-2],Lookup!C[-1],0),IFERROR(MATCH(R[0]C[-2],Lookup!C,0),MATCH(R[0]C[-2],Lookup!C[1],0)))))),""))"
    Next i


End Sub
 
Upvote 0
At the end of your formula for the IFERROR value if error you have "", change it to """".
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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