vba 如何根据单元格值启用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12729162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable button depening on cell value
提问by user1049518
I have problem. How to enable/disable a button depending on the cell value.
我有问题。如何根据单元格值启用/禁用按钮。
In excel sheet i have 2 buttons.
在 excel 表中,我有 2 个按钮。
What i need to do is.
我需要做的是。
If a column "L" having data
如果列“L”有数据
The one button enable
一键启用
else
别的
"BQ" is having data
“BQ”有数据
another button needs to be enabled. Other button will be disabled.
需要启用另一个按钮。其他按钮将被禁用。
How to achieve it.
如何实现。
Please help .... Thanks in advance
请帮助.... 提前致谢
回答by Siddharth Rout
I was waiting for your reply as to what should happen if both are filled up. I have added that option in the code. Amend it to suit your needs.
我正在等待您的回复,如果两者都填满会发生什么。我在代码中添加了该选项。修改它以满足您的需要。
Try this
尝试这个
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
CommandButton1.Enabled = False: CommandButton2.Enabled = False
'~~> If both cols are filled up
If Application.WorksheetFunction.CountA(Columns(12)) > 0 And _
Application.WorksheetFunction.CountA(Columns(69)) > 0 Then
'~~> Change the message as applicable
MsgBox "Both Columns Cannot have data", vbInformation, "Error"
Else
'~~> If Col L is filled up
If Application.WorksheetFunction.CountA(Columns(12)) > 0 _
Then CommandButton1.Enabled = True
'~~> If Col BQ is filled up
If Application.WorksheetFunction.CountA(Columns(69)) > 0 _
Then CommandButton2.Enabled = True
End If
LetsContinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
回答by mattboy
Replace CommandButton1 and CommandButton2 with whatever the names of your buttons are.
将 CommandButton1 和 CommandButton2 替换为您的按钮名称。
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.WorksheetFunction.CountA(Range("L:L")) > 0 Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
If Application.WorksheetFunction.CountA(Range("BQ:BQ")) > 0 Then
CommandButton1.Enabled = False
CommandButton2.Enabled = True
Else
CommandButton1.Enabled = True
CommandButton2.Enabled = False
End If
End Sub