0

我是 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

当在工作表函数中调用函数时,我也无法让函数返回一个值。如另一篇文章中所述,我尝试将函数的名称更改为变量,但由于预期的数组错误,我遇到了进一步的麻烦。

4

1 回答 1

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
于 2013-09-29T14:20:07.800 回答