未从worksheet_change事件调用子例程

时间:2015-03-13 12:24:34

标签: excel vba excel-vba events excel-2010

通过更改单元格中的值,可以调用宏/模块。从功能区或Alt + F8运行此宏时,它可以正常工作。

为什么来自第一个宏的调用不起作用?

Sheet1背后的宏

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Range("E25") Then
        Call GasFlow
    End If
End Sub

模块中的宏' Converse'

Sub GasFlow()


    ' GasFlow Macro
    ' This macro converts all gas units to Nm3/h

    '

            If Range("InputGasFlowUnit") = "Nm3/h" Then
                Range("GasFlowH") = Range("FeedGasFlowRate")

            ElseIf Range("InputGasFlowUnit") = "Nm3/d" Then
                Range("GasFlowH") = Range("FeedGasFlowRate") / 24

            ElseIf Range("InputGasFlowUnit") = "kg/h" Then
                Range("GasFlowH") = Range("FeedGasFlowRate") / Range("GasMolWeight") / Range("MoleInNm3")

            ElseIf Range("InputGasFlowUnit") = "kg/d" Then
                Range("GasFlowH") = Range("FeedGasFlowRate") / Range("GasMolWeight") / Range("MoleInNm3") / 24

            ElseIf Range("InputGasFlowUnit") = "kmol/h" Then
                Range("GasFlowH") = Range("FeedGasFlowRate") / Range("MoleInNm3") / 1000

            ElseIf Range("InputGasFlowUnit") = "kmol/d" Then
                Range("GasFlowH") = Range("FeedGasFlowRate") / Range("MoleInNm3") / 1000 * 24

            ElseIf Range("InputGasFlowUnit") = "SCFD" Then
                Range("GasFlowH") = Range("FeedGasFlowRate") * 0.02831685 / 24

            ElseIf Range("InputGasFlowUnit") = "MMSCFD" Then
                Range("GasFlowH") = Range("FeedGasFlowRate") * 28316.85 / 24

            ElseIf Range("InputGasFlowUnit") = "TPA" Then
                Range("GasFlowH") = Range("FeedGasFlowRate") / 1000 / 365 / Range("MoleInNm3") / Range("GasMolWeight")
            Else
                'No correct unit selected'

            End If

 End Sub

1 个答案:

答案 0 :(得分:0)

将工作表代码更改为:

更简单的替代方案(编辑1)

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$E$25" Then
        Call GasFlow
    End If
End Sub

更好的替代方案

即使多个单元格一次更改也能正常工作,包括" E25":

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("E25")) Is Nothing Then
        Call GasFlow
    End If
End Sub