VBA Excel 2010 - 范围的 LN 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21261553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Excel 2010 - LN function for a range
提问by antyinteligent
when i'm using LN function in excel it allows me to select a range, yet when trying to use the same function in vba the argument must be double, how to tackle this problem?
当我在 excel 中使用 LN 函数时,它允许我选择一个范围,但是当尝试在 vba 中使用相同的函数时,参数必须是双倍的,如何解决这个问题?
On Error Resume Next
Application.EnableEvents = False
With Sheets("Stale")
ostatnia = .Cells(6, Columns.Count).End(xlToLeft).Column
Set zakres = Range(.Cells(6, 6), .Cells(6, ostatnia))
Set zakres2 = Range(.Cells(5, 6), .Cells(5, ostatnia))
***`logarytm = Application.WorksheetFunction.Ln(zakres2)`***
nachylenie = Application.WorksheetFunction.Slope(zakres, zakres2)
MsgBox nachylenie & " $$ " & logarytm
End With
Application.EnableEvents = True
On Error GoTo 0
采纳答案by user2140261
The following will do exactly what excel is doing when the formula in normally entered:
以下将执行正常输入公式时 excel 正在执行的操作:
logarytm = Application.WorksheetFunction.Ln(CDbl(zakres2.Cells(1, 1)))
Then in VBA you could simply use:
然后在 VBA 中,您可以简单地使用:
logarytm = [Ln(A3:A9)]
In your issue if you want to use a Range Variable your simpliest solution would be:
在您的问题中,如果您想使用范围变量,您最简单的解决方案是:
logarytm = Evaluate("Ln(" & zakres2.Address & ")")
回答by simon at rcl
In Excel 2013, it's true, you can set the LN() to point to a range, but in fact it only uses that value of the first cell in the range.
在 Excel 2013 中,确实可以将 LN() 设置为指向一个区域,但实际上它仅使用该区域中第一个单元格的值。
This is what the VBA is allowing you to do: it wants the value to apply the number to. (And indeed, what is the LN of a list of numbers when put into one cell?)
这就是 VBA 允许您执行的操作:它希望将数字应用于该值。(事实上,当放入一个单元格时,一串数字的 LN 是多少?)
回答by pnuts
From Norie:
来自诺里:
LN is not a native VBA math function, but you can access it using Application.WorksheetFunction.
Log in VBA only takes one parameter.
But if you use Application.WorksheetFunction you access the LOG worksheet function which takes more parameters.
Log in VBA is the natural logarithim, whereas the LOG worksheet function has the 2nd parameter to specify the base, which if omitted defaults to 10.