Natural log VB function

Zain

New Member
Joined
Sep 4, 2007
Messages
43
Hi there. I'm having a problem using the natural log function in VB. The function is Ln.

I get the error "Sub or function not defined".

I'm using the Option Explicit ...

Here is my code (I the problem is on the line with the asterisks).
<code>
Sub MLEParameers()

'Uses data from 'Analysis' sheet to calculate severity distribution parameters for the MLE method

Dim k, n As Integer
Dim mu, sigmasq, sumlogs, sumsq As Single

n = 2150 'Total number of losses
sumlogs = 0 'sum of log losses
sumsq = 0 'sum of squared log losses

For k = 2 To n + 1
sumlogs = sumlogs + Ln(Sheets("Analysis").Cells(k, 7).Value) '<--***the problem is on this line
sumsq = sumsq + (Ln(Sheets("Analysis").Cells(k, 7).Value)) ^ 2
Next k

mu = sumlogs / n
sigmasq = sumsq - 2 * mu * sumlogs + mu ^ 2

Sheets("MLE").Range("B11").Value = mu.Value
Sheets("MLE").Range("B12").Value = sigmasq.Value

End Sub
</code>

Can you please help?

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
LN isn't a part of VBA, but you can still call it using the Application.WorksheetFunction property:

Code:
Sub MLEParameers()
'Uses data from 'Analysis' sheet to calculate severity distribution parameters for the MLE method
Dim k, n As Integer
Dim mu, sigmasq, sumlogs, sumsq As Single
n = 2150 'Total number of losses
sumlogs = 0 'sum of log losses
sumsq = 0 'sum of squared log losses
Application.WorksheetFunction
For k = 2 To n + 1
sumlogs = sumlogs + Application.WorksheetFunction.Ln(Sheets("Analysis").Cells(k, 7).Value)
sumsq = sumsq + (Application.WorksheetFunction.Ln(Sheets("Analysis").Cells(k, 7).Value)) ^ 2
Next k
mu = sumlogs / n
sigmasq = sumsq - 2 * mu * sumlogs + mu ^ 2
Sheets("MLE").Range("B11").Value = mu.Value
Sheets("MLE").Range("B12").Value = sigmasq.Value
End Sub
 
Last edited:
Upvote 0
I think it's because the code doesn't recognise what "ln" is. Try replacing every instance of ln with:
Code:
Application.WorksheetFunction.ln
Use a search and replace feature to do this.
 
Upvote 0
The VBA function Log returns the natural logarithm of a number.

LN is a worksheet function for natural log.
LOG is the worksheet function for base 10 log.
Log is the VBA function for natural log.
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,766
Members
449,336
Latest member
p17tootie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top