Logarithm is different using VBA and Excel functio

2019-01-15 16:50发布

问题:

I'm trying to get the log of a number but it looks like the Worksheet function gives one answer, and VBA another.

In VB, using Log(Range("A2")) where A2 is 7, I get 1.94591014905531, which is the correct answer. But, if in the worksheet, I use =Log(A2,10) or =Log10(a2), I get 0.84509804. What's going on here?

回答1:

VBA's Log function is the natural log. If you want log base ten you will have to use the logarithmic identity for converting bases. Like so: Log(x)/Log(10).



回答2:

Function roundit1(nn As Variant, sd As Variant)
    nn = Val(nn)
    If Not (nn = 0) Then
        xx = (1 + Int(Log(Abs(nn)) / Log(10)))
    Else
        xx = 0
    End If
    ' significant digits
    roundit = sd - xx
    If roundit < 0 Then roundit = 0
   roundit1 = Round(nn, roundit)
End Function