我收到了预期的数组错误,无法确定如何修复它。

时间:2013-09-29 14:07:02

标签: arrays excel vba

我是VBA编程的新手。我正在尝试编写一个热容量函数,可以选择温度和单位,以及物质的相位。我已经有了代码,我创建了一个宏来调试它,因为它不断得到预期的数组错误。

以下是整个代码:

Sub Macro1()
'
' Macro1 Macro
'

Dim A As Double
Dim B As Double
Dim C As Double
Dim D As Double
Dim E As Double
Dim Temp As Double
Dim Bleh As Double

    A = ThisWorkbook.Sheets("Heat Flows").Range("D3")
    B = ThisWorkbook.Sheets("Heat Flows").Range("E3")
    C = ThisWorkbook.Sheets("Heat Flows").Range("F3")
    D = ThisWorkbook.Sheets("Heat Flows").Range("G3")
    E = ThisWorkbook.Sheets("Heat Flows").Range("H3")
    Temp = ThisWorkbook.Sheets("Heat Flows").Range("I3")

Bleh = MolarHC("K", "Gas", Temp, A, B, C, D, E)

End Sub
Function TK(Opt As String, Temp As Double)
    If Opt = "C" Then
        Temp = Temp + 273.15
    ElseIf Opt = "K" Then
        Temp = Temp
    End If


End Function

Function MolarHC(Opt As String, Opt1 As String, Temp, A, B, C, D, E)

    Dim TK As Double
    Dim H As Double

    TK = TK(Opt, Temp)

Select Case Opt1

    Case "Gas"

        H = A + B * Excel.WorksheetFunction.Power(((C / TK) / Excel.WorksheetFunction.Sinh(C / TK)), 2) + D * Excel.WorksheetFunction.Power(((E / TK) / Excel.WorksheetFunction.Cosh(E / TK)), 2)

    Case "Liquid"

        H = A + B * TK + C * Excel.WorksheetFunction.Power(TK, 2) + D * Excel.WorksheetFunction.Power(TK, 3) + E * Excel.WorksheetFunction.Power(TK, 4)

    End Select


End Function

Function MassHC(Opt As String, Opt1 As String, Temp As Double, A As Double, B As Double, C As Double, D As Double, E As Double, MW As Double)

    Dim H As Double
    Dim MHC As Double

    H = MolarHC(Opt, Opt1, Temp, A, B, C, D, E)
    MHC = H / MW

End Function

在工作表函数中调用函数时,我也无法使函数返回值。我尝试将函数的名称更改为变量,如另一篇文章中所述,但我对预期的数组错误进一步陷入困境。

1 个答案:

答案 0 :(得分:0)

您需要对函数进行编码以实际返回值:

Function TK(Opt As String, Temp As Double) As Double
    If Opt = "C" Then
        TK = Temp + 273.15
    ElseIf Opt = "K" Then
        TK = Temp
    End If
End Function