禁用用户窗体上的按钮
我试图弄清楚如果电子表格中的某个单元格等于某个数字,如何禁用用户表单中的按钮。我尝试了下面所述的代码,但它不起作用。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码应该可以正常工作,因为您走在正确的道路上。
要测试它,只需创建一个新表单并添加此代码,您就会发现它应该可以工作。也许您在 IF 子句中遇到了问题?
此外,您不需要在禁用形状之前选择它;立即禁用它。
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?
Besides, you don't need to select the shape prior to disabling it; just disable it right away.
我知道这已经过时了,但进入这个线程试图解决我的问题,并找到了此处未提及的解决方案。因此,如果有人像我一样来到这里,而这并没有让他们完全到达他们需要去的地方,我认为这可能会有所帮助。
我有一个带有名为 cmdADAMFields 的下拉框的用户表单,并且我不希望启用名为 FieldsSubmitButton 的提交按钮,直到我从下拉框中选择了某些内容。
我必须将我的论点分解为两个不同的私有子语句和一个更大的 If-Then-Else 语句。
首先,我输入:
然后,当我的下拉菜单的私有子值发生变化时,我写道:
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.
I had to break up my argument into two different private subs vs one larger If-Then-Else statement.
First, I put:
Then when for my pulldown's private sub when it's value changed I wrote:
设置 Enabled 属性的正确位置是在 Activate 事件(与 Show 方法关联)中,而不是在 Initialize 事件中(与加载指令相关)。
当 AL10 单元格 >= 10 时,以下代码禁用按钮 CommandButton1。
对于按钮,您可以在普通按钮之间进行选择(属性 Enabled=False 和属性 Visible=true)、禁用按钮(属性 Enabled=False 和属性 Visible=true)和 >不可见按钮(属性Enabled=False和属性Visible=False),在大多数情况下,它是一个更干净的界面。
对于文本框,除了正常、禁用和不可见状态之外,还有锁定状态,即启用和可见,但用户无法编辑。 (属性Locked = True)
锁定的控件只能通过VBA 代码更改。例如,某人可以包含日期文本框,并使用带有日历控件的辅助弹出日期表单填充该文本框。
The proper place for setting Enabled property is in Activate event (associated with Show method) and not Initialize event (associated with Load instruction).
The below code disable the button CommandButton1 when AL10 cell >= 10.
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.
Concerning text boxes, besides normal, disabled and invisible status, there is a locked status, that is enabled and visible, but cannot be user edited. (property Locked = 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.