Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-子模块(模块)内的调用函数_Excel_Vba - Fatal编程技术网

Excel VBA-子模块(模块)内的调用函数

Excel VBA-子模块(模块)内的调用函数,excel,vba,Excel,Vba,我在VBA中具有以下功能: Public Function lorh(custo As Integer) If custo > 10.99 And custo <> 0 Then lorh = "1.4" Else If custo < 11 And custo <> 0 Then lorh = "1.35" Else If custo <= 0 Or custo < 0 Then

我在
VBA
中具有以下功能:

Public Function lorh(custo As Integer)
If custo > 10.99 And custo <> 0 Then
    lorh = "1.4"
Else
    If custo < 11 And custo <> 0 Then
        lorh = "1.35"
    Else
        If custo <= 0 Or custo < 0 Then
            lorh = "Valor Inválido"
        End If
    End If
End If
End Function
公共函数lorh(custo为整数)
如果custo>10.99且custo为0,则
lorh=“1.4”
其他的
如果custo<11且custo为0,则
lorh=“1.35”
其他的

如果custo实际上,如果您在模块中有该函数,您可以直接在工作表单元格中引用它,就像使用Excel的公式一样

=lorh(A1)
为了让代码从宏按钮运行,它需要是一个子按钮,而不是一个函数

我认为下面的代码将按照您希望的方式工作,我和Barranka一样删除了多余的部分

Public Sub lorh()
    Dim lorh As String
    custo = ActiveCell.Value

    If custo > 10.99 Then
        lorh = "1.4"
    Else
        If custo > 0 Then
            lorh = "1.35"
        Else
            lorh = "Valor Inválido"
        End If
    End If

    ActiveCell.Value = lorh
End Sub

此宏使用活动单元格值的方式与在函数中使用custo参数的方式相同。

实际上,如果模块中有该函数,则可以直接在工作表单元格中引用它,就像使用Excel公式一样

=lorh(A1)
为了让代码从宏按钮运行,它需要是一个子按钮,而不是一个函数

我认为下面的代码将按照您希望的方式工作,我和Barranka一样删除了多余的部分

Public Sub lorh()
    Dim lorh As String
    custo = ActiveCell.Value

    If custo > 10.99 Then
        lorh = "1.4"
    Else
        If custo > 0 Then
            lorh = "1.35"
        Else
            lorh = "Valor Inválido"
        End If
    End If

    ActiveCell.Value = lorh
End Sub

该宏使用活动单元格值的方式与在函数中使用custo参数的方式相同。

如果需要在excel工作表中使用函数,只需在任何单元格中写入,正如andrux所说

如果您需要从sub调用它,同样,您只需要编写它:

public sub aSubprocedure()
    ' Any variables and other instructions go here
    var = lorh(input)
    ' Your code goes on
end sub
然后,您可以将子流程分配给按钮


对代码的一些建议:

我建议对您的功能进行以下“清理”:

Public Function lorh(custo As Integer)
    If custo > 10.99 And custo <> 0 Then
        lorh = "1.4"
    Else If custo < 11 And custo <> 0 Then
        lorh = "1.35"
    Else If custo <= 0 Then
        lorh = "Valor Inválido"
    End If
End Function
公共函数lorh(custo为整数)
如果custo>10.99且custo为0,则
lorh=“1.4”
否则,如果custo<11且custo为0,则
lorh=“1.35”

否则,如果custo如果您需要在excel工作表中使用函数,您只需要在任何单元格中写入它,正如andrux所说

如果您需要从sub调用它,同样,您只需要编写它:

public sub aSubprocedure()
    ' Any variables and other instructions go here
    var = lorh(input)
    ' Your code goes on
end sub
然后,您可以将子流程分配给按钮


对代码的一些建议:

我建议对您的功能进行以下“清理”:

Public Function lorh(custo As Integer)
    If custo > 10.99 And custo <> 0 Then
        lorh = "1.4"
    Else If custo < 11 And custo <> 0 Then
        lorh = "1.35"
    Else If custo <= 0 Then
        lorh = "Valor Inválido"
    End If
End Function
公共函数lorh(custo为整数)
如果custo>10.99且custo为0,则
lorh=“1.4”
否则,如果custo<11且custo为0,则
lorh=“1.35”

否则,如果custo没问题,你是对的,但是,我想做的是像“AutoSum”这样的事情,当你点击按钮时,它会执行功能。如果你想让一个按钮或宏像AutoSum功能一样工作,你需要引用活动单元格,然后在你的函数上执行它,而这应该是一个子函数。。。看看我上面编辑的答案。好吧,你是对的,但是,我想做的是像“AutoSum”一样,当你点击按钮时,它会执行功能。如果你想让一个按钮或宏像AutoSum功能一样工作,你需要引用活动单元格,然后在你的功能中对其进行操作,这应该是一个替补。。。看看我上面编辑的答案。