VBA Userform如何修改代码以同时校验Frame4与Frame5内的勾选复选框
方案实现
你可以将需要遍历的框架放入数组中循环处理,避免重复编写逻辑,修改后的完整代码如下:
Function Addcheckboxes(emailtype) Dim Ctrl As Control Dim cont As Control Dim i As Long Dim h As Long Dim intAppCount As Integer Dim result As Long ' 新增:定义存储目标框架的数组 Dim targetFrames As Variant Dim frm As Variant ' Loop through all the applications that have been selected with the email type and then create the appropriate email template on the userform lower box If SpnColct.Count > 0 Then removeDynamicControls ' 把需要遍历的框架都加入数组 targetFrames = Array(Me.Frame4, Me.Frame5) For Each frm In targetFrames For Each Ctrl In frm.Controls If TypeOf Ctrl Is MSForms.CheckBox Then If Ctrl = True Then Call AddTextBox(Mid(Ctrl.Name, 4), emailtype) intAppCount = intAppCount + 1 End If End If Next Next If intAppCount > 1 Then Me.Frame3.Caption = "Email Templates" End Function
修改说明
- 新增了
targetFrames数组存储需要检查的Frame4、Frame5两个框架对象 - 外层新增遍历数组的循环,依次处理每个框架内的控件,原有检查勾选复选框的逻辑完全保留无需调整
如果不想用数组写法,也可以直接复制原有的Frame4遍历逻辑,把Me.Frame4.Controls改成Me.Frame5.Controls再写一遍,只是代码冗余度更高。
内容的提问来源于stack exchange,提问作者Andy M
相关产品推荐
相关产品推荐

