1

I need a little help with the below formula in VBA:

=IFERROR(VLOOKUP(J3,'Int Data'!D:J,7,0),"")

I've tried to run the following:

Range("AP2").Select    
ActiveCell.Formula = "=IFERROR((VLOOKUP(RC[-1],(J3,'Agent Table'!D:J,7,0),"")"    
Range("AP2").Select    
Selection.Copy    
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False    
Cells.Select    
Cells.EntireColumn.AutoFit

When I try to run this statement -

ActiveCell.Formula = "=IFERROR((VLOOKUP(RC[-1],(J3,'Agent Table'!D:J,7,0),"")"

I got a:

Run-time error '1004'

Can you please advise what is wrong with this statement?

Vityata
  • 42,633
  • 8
  • 55
  • 100
SeT
  • 224
  • 2
  • 13
  • 1
    Welcome to SO. My eyes could be wrong but I'm pretty sure that VLOOKUP has only 4 arguments, and in your formula you have 5. Your formula is `VLOOKUP(RC[-1],(J3,'Agent Table'!D:J,7,0)`. Should it not be `VLOOKUP(J3,'Agent Table'!D:J,7,0)` ??? That would fit your example of `VLOOKUP(J3,'Int Data'!D:J,7,0)`. 4 Arguments, not 5. – Foxfire And Burns And Burns Sep 26 '18 at 15:35
  • Besides, parenthesis quantity must be the same in both cases always, but you have 4 `(` and only 2 `)`. Something is wrong there – Foxfire And Burns And Burns Sep 26 '18 at 15:39

2 Answers2

3

You need .FormulaR1C1 and not .Formula:

ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(R3C10,'Agent Table'!C4:C10,7, 0)"""")"

R3C10 is the R1C1 notation for J3.

If you are using the Macro Recorder to see the formula, select "Use Relative References" in the developer tab to change the way the address is displayed:

enter image description here

Vityata
  • 42,633
  • 8
  • 55
  • 100
0

You are combining A1 and R1C1 referencing in the same formula, this should be avoided.

Generally R1C1 is the preferred reference style for VBA. It may require a few more keystrokes but it provides greater control when accessing the cells.

VBAjake
  • 1
  • 1