gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:40:45 28 4
gpt4 key购买 nike

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

现在我需要从 sub 开始调用这个函数,或者从宏开始调用这个函数,这样我就可以将它与 excel 上的自定义按钮相关联。工具栏。谁能指导我?

最佳答案

实际上,如果您在模块中拥有该函数,则可以直接在工作表单元格中引用它,就像使用 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 - VBA - 子(模块)内的调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12963271/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com