user3238181
user3238181

Reputation: 135

Excel function sheet gives errors

My first attempt at user-defined functions in Excel (Mac) isn't going well. This gives "function or sub undefined":

Function CylArea(d0 As Double, theta As Double, y As Double) As Double
    CylArea = Pi() * (d0 ^ 2 / 4 + d0 * Tan(Radians(theta)) * y + Tan(Radians(theta)) ^ 2 * y ^ 2)
End Function

Function CylVolume(d0 As Double, theta As Double, y As Double) As Double
    CylVolume = Pi() * (d0 ^ 2 / 4 * y + 1 / 2 * d0 * Tan(Radians(theta)) * y ^ 2 + 1 / 3 * Tan(Radians(theta)) ^ 2 * y ^ 3)
End Function

Function CylAreaDeriv(d0 As Double, theta As Double, y As Double) As Double
    CylAreaDeriv = Pi() * (d0 * Tan(Radians(theta)) + 2 * Tan(Radians(theta)) ^ 2 * y)
End Function

Curiously, the Pi in CylVolume is highlighted.

Upvotes: 0

Views: 39

Answers (1)

Soulfire
Soulfire

Reputation: 4296

It looks like you are attempting to use Excel functions as you would in a worksheet within VBA code. This will likely not work.

Try changing all instances of Pi() to WorksheetFunction.Pi, all instances of Tan to Math.Tan and all instances of Radians to WorksheetFunction.Radians so that the code looks more like this:

Function CylVolume(d0 As Double, theta As Double, y As Double) As Double
    CylVolume = WorksheetFunction.Pi * (d0 ^ 2 / 4 * y + 1 / 2 * d0 * Math.Tan(WorksheetFunction.Radians(theta)) * y ^ 2 + 1 / 3 * Math.Tan(WorksheetFunction.Radians(theta)) ^ 2 * y ^ 3)
End Function

Upvotes: 1

Related Questions