【问题标题】:How to force a user to select an option, using frames, excel userform如何强制用户选择一个选项,使用框架,excel userform
【发布时间】:2025-12-02 17:55:02
【问题描述】:

我的用户窗体上有 4 个框架。

第 1、2 和 4 帧有两个选项按钮。 第 3 帧有 5 个选项按钮。

What I want to do, is that when the command button is selected, that if an option button in a frame has not been selected, a message will appear.

我希望每个帧都有一条自定义消息。

我已经开始了,但真的很难掌握不同的框架......有人能指出我正确的方向吗? (仍然是初学者并正在努力学习,所以如果所有回复都可以简化,那就太好了!: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"

【问题讨论】:

  • 您是否尝试使用嵌套选项?就像如果选择了MasterOption1,则检查至少选择了SlaveOptions 之一。然后将主选项拆分为帧?截屏您的表单可能是一个想法
  • 嗨 SilverShotBee -- 感谢您的评论。我听取了您的建议,并在 OP 中附上了一张照片。您可以在顶部看到四个框架——现有客户/年龄/性别/产品查询。感谢您对如何使其发挥作用的帮助和想法!

标签: excel userform vba


【解决方案1】:

尝试以下方法:

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

对每一帧执行相同的操作,将 [ControlName] 替换为控件的名称,并将 [Message] 和 [Message Box Name] 替换为您想要的自定义消息。 对于第 3 帧,您需要包含额外的 3 个“与”语句。

【讨论】:

  • 成功了。谢谢!我现在有一个相关的问题,我希望您的代码可以与我的多页一起使用...*.com/questions/24699926/…
【解决方案2】:

可能不是最优雅的解决方案,但效果很好。循环浏览每一帧(您需要更新帧名称以匹配您的 [名称,而不是标题!]),然后确保至少设置了一个选项,否则显示一条消息。按需调整

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

【讨论】:

  • 感谢 SilverShotBee。我使用了代码,但 +voted 你的解决方案。我现在有另一个相关的问题..也许你可以帮忙? *.com/questions/24699926/…
最近更新 更多