Excel macro formula adding quotes in formula causing vlookup to not work

妖精的绣舞 提交于 2019-11-26 18:39:19

问题


I am trying to add a formula to a cell. The macro is adding quotations around the cell range, which causes the cell to return the #name error.

This is the code in the macro

Cells(1, 4).FormulaR1C1 = "=VLOOKUP(RC[-2],Contracts!A7:D30,3,FALSE)"

This is what the code returns in Excel after executing the macro.

=VLOOKUP(B4,Contracts!'A7':'D30',3,FALSE) 

Note that #name error disappears and the formula works, after I remove the quotations around the cell A7 and D30.


回答1:


You cannot mix RC and A1 style references in the same formula. Pick one and stick to it.

"=VLOOKUP(RC[-2],Contracts!R7C1:R30C4,3,FALSE)"

will work




回答2:


The RC[-2] seems to be the problem. When I replaced that with a normal cell reference the macro didn't put quotes around the A7 and D30.




回答3:


I believe the problem is caused by mixing RC notation (FormulaR1C1) and A1. Have you tried:

Cells(1, 4).FormulaR1C1 = "=VLOOKUP(RC[-2],""Contracts!A7:D30"",3,FALSE)"

? That should work.



来源:https://stackoverflow.com/questions/8164867/excel-macro-formula-adding-quotes-in-formula-causing-vlookup-to-not-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!