excelvbamacosexcel-2011

How to make a Cosd Function with VBA for Excel?


I need to make a function that finds Cosine in Degrees. I am on Mac-OS Excel 2011

Function cosd(d)
    deg = (d * 3.14159265358979) / 180    
    cosd = WorksheetFunction.Cos(deg)
End Function

It still does not work.


Solution

  • You are really close the only trouble with your function (well, besides that not declaring your variables is bad practice) is that Cos isn't a VBA function. Change your function to

    Function cosd(d As Double) As Double
      cosd = Application.WorksheetFunction.Cos((d * Application.WorksheetFunction.Pi) / 180#)
    End Function
    

    and it should work just fine.