简体   繁体   English

如何将此Excel公式转换为VBA代码?

[英]How do I convert this Excel formula into VBA code?

'I am using this formula in an Excel worksheet, in cell A6. '我在A6单元格的Excel工作表中使用此公式。 It is working fine. 一切正常。

=IF(O6="Hand","Manual Entry",IF(O6="JET",R6,IF(O6="COKE","Red Bull",IF(O6="Freight","Logistics",IF(O6="TAX","Tax",IF(O6="TRANSFER COST","Transfer Cost Transactions",IFERROR(IF(FIND("INV#",R6,1)>=1,MID(R6,FIND("INV#",R6,1),10),""),""))))))) = IF(O6 =“ Hand”,“ Manual Entry”,IF(O6 =“ JET”,R6,IF(O6 =“ COKE”,“ Red Bull”,IF(O6 =“ Freight”,“ Logistics”,IF (O6 =“ TAX”,“税收”,IF(O6 =“ TRANSFER COST”,“转移成本交易”,IFERROR(IF(FIND(“ INV#”,R6,1)> = 1,MID(R6,FIND (“ INV#”,R6,1),10),“”),“”))))))))))

Now, my question is: how do I convert this to VBA? 现在,我的问题是:如何将其转换为VBA? I have tried recording it, and the code is as follows: 我尝试记录它,代码如下:

ActiveCell.FormulaR1C1 = _ "=IF(RC[14]=""Hand"",""Manual Entry JE"",IF(RC[14]=""JET"",RC[17],IF(RC[14]=""COKE"",""Red Bull"",IF(RC[14]=""FREIGHT"",""Logistics"",IF(RC[14]=""TAX"",""Tax"",IF(RC[14]=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",RC[17],1)>=1,MID(RC[17],FIND(""INV#"",RC[17]" & _ """""),"""")))))))" ActiveCell.FormulaR1C1 = _“ = IF(RC [14] =”“ Hand”“,”“手动输入JE”“,IF(RC [14] =”“ JET”“,RC [17],IF(RC [ 14] =“” COKE“”,“” Red Bull“”,IF(RC [14] =“” FREIGHT“”,“”物流“”,IF(RC [14] =“” TAX“”,“”税收”“,IF(RC [14] =”“ TRANSFER COST”“,”“转移成本交易”“,IFERROR(IF(FIND(”“ INV#”“,RC [17],1)> = 1, MID(RC [17],FIND(“” INV#“”,RC [17]“&_”“”“”),“”“”))))))))))“

When I run this, I am receiving Run Time Error 1004: Application-defined or object-defined error.So I Changed this to something like this, this doing same as above formula except the find option, everything is running fine. 当我运行此程序时,我收到运行时错误1004:应用程序定义的错误或对象定义的错误,因此我将其更改为类似的内容,除find选项外,其他操作与上述公式相同,一切运行正常。 ![VBA For Above formula][Any Help on the find?] End sub.How do i get the fine option in the above VBA code.` ![VBA对于上述公式] [是否有任何帮助?]结束子项。如何在上述VBA代码中获得优良的选择。

This works for me: 这对我有用:

Range("L6").Formula = "=IF(O6=""Hand"",""Manual Entry"",IF(O6=""JET"",R6,IF(O6=""COKE"",""Red Bull"",IF(O6=""Freight"",""Logistics"",IF(O6=""TAX"",""Tax"",IF(O6=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",R6,1)>=1,MID(R6,FIND(""INV#"",R6,1),10),""""),"""")))))))"

This is just your original Excel formula, but with the " characters escaped as "" . 这只是您原始的Excel公式,但带有"字符转义为""字符。

You need to escape your quotes. 您需要转义报价。

ActiveCell.Formula = "=IF(O6=""Hand"",""Manual Entry"",IF(O6=""JET"",R6,IF(O6=""COKE"",""Red Bull"",IF(O6=""Freight"",""Logistics"",IF(O6=""TAX"",""Tax"",IF(O6=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",R6,1)>=1,MID(R6,FIND(""INV#"",R6,1),10),""""),"""")))))))"

And use .Formula since you're using specific cell names that are not relative to the currently selected cell. 并使用.Formula因为您使用的是与当前所选单元格无关的特定单元格名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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