VBA sin() 和 cos() 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21971654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:05:36  来源:igfitidea点击:

VBA sin() and cos() trouble

excelvbatrigonometry

提问by Vasilii Burlacu

Im trying to make some VBA form and to calculate some simple values but I dont understand why VBA dosen't recognize Sin() and Cos() functions. For example: I wrote int() and VBA transformed it into Int(), but it doesn't do the same with sin and cos. Moreover, after I'm trying to compile my code

我试图制作一些 VBA 形式并计算一些简单的值,但我不明白为什么 VBA 不识别 Sin() 和 Cos() 函数。例如:我写了 int() 并且 VBA 将它转换为 Int(),但它对 sin 和 cos 不做同样的事情。此外,在我尝试编译我的代码之后

    Private Sub btn_exit_Click()
    Unload laborator_2
End Sub

Private Sub btn_start_Click()
    today.Caption = Date
    rnb_val.Caption = Int(Rnd * 90 + 1)
    sqrt_val.Caption = Sqr(rnb_val.Caption)
    si_val.Caption = sin(rnb_val.Caption * 3.14159 / 180)
    co_val.Caption = rnb_val.Caption * 3.1459 / 180
End Sub

I got this message:

我收到了这条消息:

Run-time error "424" Object required

enter image description here

在此处输入图片说明

Im new in VBA, so sorry if I didn't write all details. Thank you.

我是 VBA 新手,如果我没有写下所有细节,很抱歉。谢谢你。

回答by Dale M

sin & cos are not functions that are built into vba. They are, however, built into excel and can be accessed through the $WorksheetFunction$ object of vba as $WorksheetFunction.Sin$ and $WorksheetFunction.Cos$. Any functions that you can use in a spreadsheet are accessible this way.

sin & cos 不是内置在 vba 中的函数。但是,它们内置于 excel 中,可以通过 vba 的 $WorksheetFunction$ 对象作为 $WorksheetFunction.Sin$ 和 $WorksheetFunction.Cos$ 访问。您可以在电子表格中使用的任何函数都可以通过这种方式访问​​。

回答by paul

Check the names that you have given to your labels on the form - they must match exactly the names in the code. I got the same error by naming the si_val label sival instead.

检查您在表单上为标签指定的名称 - 它们必须与代码中的名称完全匹配。我通过将 si_val 标签命名为 sival 得到了同样的错误。

回答by airdas

I had to add Imports System.Mathat the top to make the compiler recognise Sin, Cos etc. (which I found here in the docs).

我必须Imports System.Math在顶部添加以使编译器识别 Sin、Cos 等(我在文档中找到了这些)。