功能中的数据类型错误

时间:2016-06-07 14:10:19

标签: vba excel-vba excel

我变得非常讨厌"此公式中使用的值是错误的数据类型。"错误,我不能为我的生活找出造成它的原因。

Public Function Cc(q As Double, ID As Double) As Double

Dim YP As Double            'lb/100ft^2
Dim Fan600 As Single
Dim Fan300 As Single
Dim n As Double             'Flow behavior index
Dim K As Double             'Consistency index

Dim Form As Worksheet
Set Form = ThisWorkbook.Worksheets("ECD Generator")

'=========================================================================================================

YP = Form.Cells(5, 10).Value
Fan600 = Form.Cells(6, 10).Value
Fan300 = Form.Cells(7, 10).Value

'=========================================================================================================

n = 3.32 * WorksheetFunction.Log10(Fan600 / Fan300)
K = 5.1 * Fan600 / (1022 ^ n)

Cc = 1 - (1 / (2 * n + 1)) * (YP / (YP + K * ((3 * n + 1) * q / (n * WorksheetFunction.Pi * (ID / 2) ^ 3)) ^ n))

End Function

2 个答案:

答案 0 :(得分:3)

根据您在评论中提供的值,我得到了不同的错误

  

16:表达式过于复杂

可以找到有关此错误的信息here。基本上,在一次计算中会发生太多的数学运算。

为了解决这个问题,我把它Cc分成两部分,这就错过了。我意识到这不是你在问题中显示的错误,但它们很有可能是相关的。

请尝试以下替换Cc代码行

Cc = n * WorksheetFunction.Pi * (ID / 2) ^ 3
Cc = 1 - (1 / (2 * n + 1)) * (YP / (YP + K * ((3 * n + 1) * q / Cc) ^ n))

要确认,这是用两行替换以Cc =开头的单行。

答案 1 :(得分:2)

在Excel 2013中运行此代码时,Cc = ...表达式会抛出“表达式太复杂”错误。使用临时变量简化表达式:

n = 3.32 * WorksheetFunction.Log10(Fan600 / Fan300)
K = 5.1 * Fan600 / (1022 ^ n)

'Add these temp variables to make the "Cc = ..." expression less complex.

Dim t1 As Double
t1 = 1 / (2 * n + 1)

Dim t2 As Double
t2 = n * WorksheetFunction.Pi * (ID / 2) ^ 3

Cc = 1 - t1 * (YP / (YP + K * ((3 * n + 1) * q / t2) ^ n))
'Cc = 1 - (1 / (2 * n + 1)) * (YP / (YP + K * ((3 * n + 1) * q / (n * WorksheetFunction.Pi * (ID / 2) ^ 3)) ^ n))