禁用用户窗体上的按钮
问题描述:
如果我的电子表格中的某个单元格等于某个数字,我正在尝试弄清楚如何禁用我的用户窗体中的按钮.我尝试了下面所述的代码,但它不起作用.
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
答
您的代码应该可以正常工作,因为您走在正确的道路上.
Your code should be working, as you're on the right path.
要测试它,只需创建一个新表单并添加此代码,您就会看到它应该可以工作.也许您在 IF 子句中遇到了问题?
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.
Private Sub UserForm_Initialize()
CommandButton1.Enabled = False
End Sub