![](/img/trans.png)
[英]Excel VBA - Custom Function; #VALUE error; VLOOKUP on different worksheet
[英]Creating a custom function with VBA Excel, error #VALUE
Public Function CopperThermalConductivity(T As Double, RRR As Double)
Dim P1, P2, P3, P4, P5, P6, P7 As Double
Dim W0, W1, W10 As Double
Dim Beta, BetaR As Double
Beta = 0.634 / RRR
BetaR = Beta / 0.0003
P1 = 0.00000001754
P2 = 2.763
P3 = 1102
P4 = -0.165
P5 = 70
P6 = 1.756
P7 = 0.838 / (BetaR ^ 0.1661)
W0 = Beta / T
W1 = (P1 * (T ^ P2)) / (1 + (P1 * P3 * (T ^ (P2 + P4)) * WorksheetFunction.Exp(-(P5 / T) ^ P6)) + W0)
W10 = (P7 * W1 * W0) / (W1 + W0)
CopperThermalConductivity = (1 / (W0 + W1 + W10))
End Function
Sub DescribeFunctionCopper()
Dim FuncName As String
Dim FuncDesc As String
Dim ArgDesc(1 To 2) As String
FuncName = "CopperThermalConductivity"
FuncDesc = "Returns the Thermal Conductivity in W/m-K"
ArgDesc(1) = "Temperature in Kelvin"
ArgDesc(2) = "Residual-resistivity ratio"
Application.MacroOptions _
Macro:=FuncName, _
Description:=FuncDesc, _
ArgumentDescriptions:=ArgDesc, _
Category:="Cryogenics"
End Sub
我写了一些代码来创建Excel的可访问函数。 不知何故,当我在Excel上使用此功能时,出现#VALUE
错误,知道吗? 构建宏时没有任何错误。 请帮帮我。
此行是错误的来源:
W1 = (P1 * (T ^ P2)) / (1 + (P1 * P3 * (T ^ (P2 + P4)) * WorksheetFunction.Exp(-(P5 / T) ^ P6)) + W0)
您不能在VBA代码中使用工作表功能EXP
。 有等效的VBA Exp()
,应使用它代替工作表函数。
顺便说一句,请注意,当您声明这样的变量时:
Dim P1, P2, P3, P4, P5, P6, P7 As Double
Dim W0, W1, W10 As Double
Dim Beta, BetaR As Double
仅P7
, W10
和BetaR
声明为Double
,其他声明为Variant
。 如果要将所有这些变量声明为Double
,则应这样做:
Dim P1 As Double, P2 As Double, P3 As Double, P4 As Double, P5 As Double, P6 As Double, P7 As Double
Dim W0 As Double, W1 As Double, W10 As Double
Dim Beta As Double, BetaR As Double
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.