简体   繁体   English

如何将 Excel Function 变成 VBA 代码?

[英]How Can I Turn Excel Function Into VBA Code?

I am looking to convert an Excel function into VBA code to work in a button but am having some trouble as I am new to coding.我正在寻找将 Excel function 代码转换为 VBA 代码以在按钮中工作,但由于我是编码新手,所以遇到了一些麻烦。 I have tried looking up code that would multiply cells and do what I need it to but have had no luck implementing them into my code.我已经尝试查找可以增加单元格并执行我需要的代码,但是没有运气将它们实现到我的代码中。 Below is the function that I am trying to turn into code.下面是我试图转换成代码的 function。 Any help or suggestions would be greatly appreciated.任何帮助或建议将不胜感激。

=SUM(G17-(G16*0.65)) =SUM(G17-(G16*0.65))

Public Function Sum()

Range("G17").Value -Range("G16").Value * 0.65

End Function

I think that you want to replace Excel WorkSheet function by your own VBA function, this is excellent to develop complicated UDF to use in your VBA projects. I think that you want to replace Excel WorkSheet function by your own VBA function, this is excellent to develop complicated UDF to use in your VBA projects.

First you should create a Public Module, named for example basMyModule .首先,您应该创建一个公共模块,例如命名为basMyModule

In this module you add this code在此模块中,您添加此代码

Option Explicit

Function mySum()

  mySum = Range("G17").Value - Range("G16").Value * 0.65

End Function

The keyword Public is implicit in a global module, so useless.关键字 Public 隐含在全局模块中,因此无用。 At the end of the function you assign mySum to the value to return in VBA.在 function 的末尾,您将mySum分配给要在 VBA 中返回的值。

Now mySum () is a function you can use in an Excel cell, replacing Worksheet function Sum() like this:现在mySum () 是 function 您可以在 Excel 单元格中使用,像这样替换 Worksheet function Sum() :

=mySum()

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

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