VBA userform command botton限定

我的userform里有两个optionbotton, 两个textbox,我想限定两个textbox都输入了数据,且optionbotton也做了选择,三个条件都满足的情况下,且这三个数据都需要写入另一个excel表里,userform里的command botton才可以点击,否则为灰色,且整个userform不能点右上角的小叉关闭。麻烦这段代码应该怎么写呢,谢谢!

第1个回答  2014-10-06
对于右上角的 小叉关闭 失效功能,请使用如下代码:
Private Sub UserForm_queryclose(cancel As Integer, closemode As Integer)
If closemode = 0 Then
cancel = True
End If
End Sub
如果自己定义一个按钮关闭窗体,可以加入如下代码:
Private Sub CommandButton1_Click()
UserForm1.Hide
End Sub
-----------------------------------------------
对于满足 2个Textbox和 Optionbotton按钮做了选择,才可以点击 Command2 button 按钮,请使用如下代码:

Private Sub UserForm_Initialize()
CommandButton2.Enabled = False
End Sub

Private Sub TextBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If OptionButton1.Value = True Or OptionButton2.Value = True Then
If TextBox2.Text <> "" And TextBox1.Text <> "" Then
CommandButton2.Enabled = True
Else
CommandButton2.Enabled = False
End If
Else
CommandButton2.Enabled = False
End If
End Sub本回答被提问者和网友采纳