简体   繁体   中英

Run-time error 1004 for Vlookup

I'm getting a run-time error on the following code and not sure what's causing it. The code is meant to run a vlookup for cells, but it returns a blank instead of a "0" if the lookup does not exist.

ActiveCell.FormulaR1C1 = "=IF(LEN(VLOOKUP(RC[-3], Schedule!C[-3]:C[-2], 2,FALSE))=0,"",VLOOKUP(RC[-3], Schedule!C[-3]:C[-2], 2,FALSE))"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G600"), Type:=xlFillDefault
Range("G2:G600").Select

I'm sure it's something minor, but it's driving me nuts and i can't force the program to continue regardless of the error

Your double quotes are syntactically right for VBA but false for the resulting formula. The VBA string "=IF(...,"",...)" is resulting in =IF(...,",...) which is not correct syntax for an Excel formula.

Correct should be:

ActiveCell.FormulaR1C1 = "=IF(LEN(VLOOKUP(RC[-3], Schedule!C[-3]:C[-2], 2,FALSE))=0,"""",VLOOKUP(RC[-3], Schedule!C[-3]:C[-2], 2,FALSE))"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM