vba 禁用用户窗体上的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5556070/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:57:31  来源:igfitidea点击:

Disable button on a userForm

excelvbauserform

提问by Mike

I'm trying to figure out how to disable a button within my userForm if a certain cell within my spreadsheet equals a certain number. I tried the code stated below, but it isn't working.

我试图弄清楚如果我的电子表格中的某个单元格等于某个数字,如何禁用我的用户窗体中的按钮。我尝试了下面所述的代码,但它不起作用。

Private Sub UserForm_Initialize()
Label2 = Sheets("DATA").Range("AM2").Value
Label4 = Sheets("DATA").Range("AO2").Value
Label7 = Format(Sheets("DATA").Range("R8").Value, "Currency")

If Sheets("DATA").Range("AL10").Value = 10 Then
ActiveSheet.Shapes("CommandButton1").Select
UserFormact_Upgrade.CommandButton1.Enabled = False

Else


End If

End Sub

回答by Tiago Cardoso

Your code should be working, as you're on the right path.

您的代码应该可以正常工作,因为您走在正确的道路上。

To test it, simply create a new form and add this code, you'll see it should work. Maybe you're having problems within the IF clause?

要测试它,只需创建一个新表单并添加此代码,您就会看到它应该可以工作。也许您在 IF 子句中遇到了问题?

Besides, you don't need to select the shape prior to disabling it; just disable it right away.

此外,您不需要在禁用它之前选择形状;只需立即禁用它。

Private Sub UserForm_Initialize()

    CommandButton1.Enabled = False

End Sub

回答by Gary Nolan

I know this is old, but got to this thread trying to solve my problem, and found a solution that wasn't mentioned here. So in case someone gets here like I did, and this didn't quite get them where they needed to go, I thought this might help.

我知道这很旧,但是尝试解决我的问题时找到了该线程,并找到了此处未提及的解决方案。因此,如果有人像我一样到达这里,而这并没有让他们到达需要去的地方,我认为这可能会有所帮助。

I had a userform with a drop down box called cmdADAMFields, and I didn't want my submit button called FieldsSubmitButton to be enabled until I selected something from the dropdown box.

我有一个带有名为 cmdADAMFields 的下拉框的用户表单,在我从下拉框中选择某些内容之前,我不希望启用名为 FieldsSubmitButton 的提交按钮。

I had to break up my argument into two different private subs vs one larger If-Then-Else statement.

我不得不将我的论点分解为两个不同的私人潜艇和一个更大的 If-Then-Else 语句。

First, I put:

首先,我提出:

Private Sub UserForm_Activate()
If cmbADAMFields.ListIndex = -1 Then FieldsSubmitButton.Enabled = False
End Sub

Then when for my pulldown's private sub when it's value changed I wrote:

然后当我的下拉的私有子值改变时,我写道:

Private Sub cmbADAMFields_Change()
FieldsSubmitButton.Enabled = True
End Sub

回答by Paulo Buchsbaum

The proper place for setting Enabledproperty is in Activateevent (associated with Showmethod) and not Initializeevent (associated with Loadinstruction). The below code disable the button CommandButton1when AL10 cell >= 10.

设置Enabled属性的正确位置 是在Activate事件(与Show方法相关)而不是Initialize事件(与Load指令相关)。下面的代码在 AL10 单元格 >= 10 时禁用按钮CommandButton1

        Private Sub UserForm_Activate()
           CommandButton1.Enabled = ( Sheets("DATA").Range("AL10") < 10 )
        End Sub

For buttons you can choose between normal buttons(property Enabled=False and property Visible=true), disabled buttons(property Enabled=False and property Visible=true) and invisible buttons(property Enabled=False and property Visible=False), that it is a cleaner interface, in most cases.

对于按钮,您可以在普通按钮(属性Enabled=False 和属性Visible=true)、禁用按钮(属性Enabled=False 和属性Visible=true)和不可见按钮(属性Enabled=False 和属性Visible=False)之间进行选择,在大多数情况下,是一个更干净的界面。

Concerning text boxes, besides normal, disabledand invisiblestatus, there is a lockedstatus, that is enabled and visible, but cannot be user edited. (property Locked= True)

关于文本框,除了正常禁用不可见状态外,还有一个锁定状态,即启用和可见,但不能被用户编辑。(属性锁定= True)

A locked control only can be changed by VBA code. For instance, someone can includes date text boxes, that it's filled using a secondary popup date form with Calendar control.

锁定的控件只能通过 VBA 代码更改。例如,某人可以包含日期文本框,它是使用带有日历控件的辅助弹出日期表单填充的。