如何强制用户select一个选项,使用框架,excel用户窗体

在这里输入图像说明 我有我的用户表单4帧。

帧1,2和4有两个选项button。 第3帧有5个选项button。

我想要做的是,当select了命令button时,如果一个选项框中的选项button没有被选中,就会出现一条消息。

我想为每个框架有一个自定义消息。

我已经做了一个开始,但是真正努力去应付不同的框架……有人能指出我正确的方向吗? (还是非常初学者,想学习,所以如果所有的回复都可以被贬低,那就太棒了!)D)

(我从一个例子中拉出了这个代码,所以它可能不是我的问题的最佳方法…)

Dim ThisControl As Control For Each ThisControl In UserForm2.Frame1.Controls If TypeName(ThisControl) = "OptionButton" And _ ThisControl.Value = True Then Unload Me End If Next ThisControl MsgBox "Please Select an Option", vbCritical, "Select Opton" 

尝试以下操作:

 If Me.[ControlName].Value = False And Me.[ControlName].Value = False Then MsgBox "[Message]", vbExclamation, "[Message Box Name]" Exit Sub End If 

对每个框架执行相同操作,用控件名称replace[ControlName],并用所需的自定义消息replace[Message]和[Message Box Name]。 对于第3帧,您将需要包含额外的3'和'语句。

可能不是最优秀的解决scheme,但它工作正常。 循环遍历每一帧(您将需要更新帧名称以匹配您的[Name,not caption!]),然后确保至less设置了一个选项,否则显示一条消息。 根据需要调整

 Private Sub CommandButton1_Click() For Each ctrl In Frame1.Controls If TypeOf ctrl Is msforms.OptionButton Then If ctrl.Value = True Then F1D = True Exit For End If End If Next ctrl If F1D = False Then MsgBox "No Option Selected In Frame 1" End If For Each ctrl In Frame2.Controls If TypeOf ctrl Is msforms.OptionButton Then If ctrl.Value = True Then F2D = True Exit For End If End If Next ctrl If F2D = False Then MsgBox "No Option Selected In Frame 2" End If For Each ctrl In Frame3.Controls If TypeOf ctrl Is msforms.OptionButton Then If ctrl.Value = True Then F3D = True Exit For End If End If Next ctrl If F3D = False Then MsgBox "No Option Selected In Frame 3" End If For Each ctrl In Frame4.Controls If TypeOf ctrl Is msforms.OptionButton Then If ctrl.Value = True Then F4D = True Exit For End If End If Next ctrl If F4D = False Then MsgBox "No Option Selected In Frame 4" End If End Sub 

在这里输入图像说明