excel - 如何禁用工作表上的按钮?
问题描述
我正在尝试禁用工作表上的按钮。
我可以让它不可见,但我宁愿把它放在屏幕上,但不能点击和变灰,所以它显然是不可点击的。
这是我的按钮脚本:
Sub LinkFile()
Call Module1.setup
Dim btn1 As Button
Dim t As Range
Set t = Range(Cells(3, "G"), Cells(4, "I"))
Set btn1 = ACImport.Buttons.Add(Left:=t.Left, Top:=t.Top, Width:=t.Width, Height:=t.Height)
btn1.Name = "Button1"
btn1.OnAction = "FileSearch" ' MySub is executed when btn is clicked
btn1.Caption = "GO!"
End Sub
解决方案
一种解决方法可能是“禁用”这样的按钮
Sub DisableBtn()
Dim btn As Button
Dim wks As Worksheet
Set wks = ActiveSheet ' replace with the sheetname the button is on
Set btn = wks.Buttons("Button1")
btn.Font.ColorIndex = 15
btn.Enabled = False
End Sub
但是您仍然可以单击该按钮,因此您需要进行Filesearch
类似的修改
Sub FileSearch()
Dim btn As Button
Dim wks As Worksheet
Set wks = ActiveSheet
Set btn = wks.Buttons("Button1")
If btn.Enabled Then
' Your action here
MsgBox "FileSearch"
End If
End Sub
要再次“启用”按钮,请使用此代码
Sub Enablebtn()
Dim btn As Button
Dim wks As Worksheet
Set wks = ActiveSheet ' replace with the sheetname the button is on
Set btn = wks.Buttons("Button1")
btn.Font.ColorIndex = 1
btn.Enabled = True
End Sub
如前所述,这是一种解决方法,仍然可以单击灰色按钮:-(
推荐阅读
- javascript - 如何在 js 中显示字形图标?
- css - 如何使用 css 将帖子内容中的所有粗体文本转换为 h2 或 h3 标签?
- android - 带有材料组件的 FAB 看起来是方形的
- c++ - 需要特定参数包大小的 C++ 模板概念
- android - 有没有办法在键盘打开时只跳转edittext而不是完整的布局?
- javascript - 如果父元素可见,则显示子元素
- reactjs - 如何知道什么改变了 React State
- sql - 如何在一条 SQL 语句中组合两个不同的 SQL 查询?
- r - 创建每月吸收率图
- excel - 如何使用单个公式计算条件中位数数组的平均值?