VBA sin() and cos() trouble

爷,独闯天下 提交于 2019-12-13 04:03:01

问题


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

    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

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


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/21971654/vba-sin-and-cos-trouble

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