Insert Excel Formula via VBA

≯℡__Kan透↙ 提交于 2020-01-30 10:35:32

问题


as a continuation from another question, I'm trying to solve my problems inserting a formula via VBA on a macro.

Here's my code:

Range("F1").Select
ActiveCell.Formula = "=IF(C1=""LPPD"";""MIPRU"";IF(C1=""LPGR"";""DCT"";IF(OR(C1=""LPFL"";C1=""LPCR"");""LADOX"";IF(OR(C1=""LPPI"";C1=""LPSJ"";C1=""LPHR"");""NOTMA"";""ERRO""))))"

For some reason, and the code doesn't show any errors, when I try to run it I get:

Run-time error ("Application-defined or object-defined error")

Worth mentioning I'm using Excel 2003.

Hope I'll be able to find my answer with you guys! Thanks in advance.


回答1:


VBA is US-EN centric, so using the .Formula the formulas must be with , instead of ;:

Range("F1").Formula = "=IF(C1=""LPPD"",""MIPRU"",IF(C1=""LPGR"",""DCT"",IF(OR(C1=""LPFL"",C1=""LPCR""),""LADOX"",IF(OR(C1=""LPPI"",C1=""LPSJ"",C1=""LPHR""),""NOTMA"",""ERRO""))))"

Or you can use .FormulaLocal

Range("F1").FormulaLocal = "=IF(C1=""LPPD"";""MIPRU"";IF(C1=""LPGR"";""DCT"";IF(OR(C1=""LPFL"";C1=""LPCR"");""LADOX"";IF(OR(C1=""LPPI"";C1=""LPSJ"";C1=""LPHR"");""NOTMA"";""ERRO""))))"


来源:https://stackoverflow.com/questions/47395256/insert-excel-formula-via-vba

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