gpt4 book ai didi

vba - Excel VBA-单击时如何在多个宏上调用命令按钮?

转载 作者:行者123 更新时间:2023-12-04 21:38:53 25 4
gpt4 key购买 nike

我正在尝试在 Excel VBA 中编写一个命令按钮,该按钮可以根据单元格的字符串值调用特定的宏。我以为我可以只使用条件“if”语句,但程序中有一个错误。

例如,到目前为止,我有:

Private Sub CommandButton1_Click()
If Range("F3").Select = "Retirement Age" Then
Call NewRA
End If
End Sub

当单元格 F3 等于“退休年龄”时,我希望 Button 运行名为“NewRA”的宏。我怎样才能适本地编写代码呢?提前致谢。

最佳答案

首先,我建议使用 Range("F3").Value而不是 Range("F3").Select .

从那里开始,如果您打算将两个以上的脚本连接到此按钮,我会阅读 Select Case。构造。这是一个 MSDN 链接:http://msdn.microsoft.com/en-us/library/cy37t14y.aspx

例如:

'... do your setup here

Select Case CStr(Range("F3").Value) '<~ convert whatever is in cell F3 to a String
Case "Retirement Age"
Call NewRA
Case "Another String"
Call AnotherStringScript
Case "Another, Even Cooler String"
Call AnotherEvenCoolerString
'...
Case Else
Msgbox ("Hey! The value in this cell doesn't match a script!")
End Select

关于vba - Excel VBA-单击时如何在多个宏上调用命令按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24391837/

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