multipage - 无法验证框架内的选项按钮 - excel VBA

时间:2014-07-11 14:18:54

标签: excel vba excel-vba

我有一个两页的用户表单,所有目前都正常工作(差不多)。

当我的用户点击第一页上的命令按钮时,我希望只有选择了特定的选项按钮才能将它们发送到第二页:

 If ProductEnquiryYes.Value = True Then
Me.MultiPage1.Value = 1
closeForm = False
Cells(emptyRow, 20).Value = 1
End If

因此,如果选中产品查询选项,则应转到下一页。 如果未选中产品查询选项,则用户应结束表单。 (标签标题将被隐藏)

但是,我现在想要在第一页的框架中验证选项,以便必须选择每个框架内的选项。我几乎就在那里,但如果用户没有选择选项,则会出现警告,但用户会转到下一页。 (即用户收到警告太晚了)

enter image description here

在我决定是否将用户发送到下一页或关闭表单之前,我需要检查选项按钮是否已经过验证。

有人可以帮忙吗?希望我的问题很明确。

Private Sub CommandButton1_Click()

Dim emptyRow As Long

Dim closeForm As Boolean

' we assume we want the form closed unless there is a reason to go to the Extra tab
closeForm = True


'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Did customer ask about product?

If ProductEnquiryYes.Value = True Then
Me.MultiPage1.Value = 1
closeForm = False
Cells(emptyRow, 20).Value = 1
End If

If ProductEnquiryNo.Value = True Then
    Cells(emptyRow, 21).Value = 1
End If

'=========================================================
'Services
'=========================================================

'Balance Enquiry
If BalanceEnquiry.Value = True Then
    Cells(emptyRow, 23).Value = 1
End If


'==========================================================
'Ensure Options in the frames are selected

If Me.YesCustomerOption.Value = False And Me.NoCustomerOption.Value = False Then
    MsgBox "Please ensure you have selected an option for 'Is the customer an existing ASB customer?'", vbExclamation, "Failed to select an option"
    Exit Sub
End If

'==========================================================

'Close Userform
If closeForm Then Unload Me


End Sub

1 个答案:

答案 0 :(得分:1)

切换到第二页的代码行在您检查之前,以确保表单已经过验证。如果您想在执行任何其他操作之前确保满足这两个条件,则应将此部分代码移到更早的位置:

If Me.YesCustomerOption.Value = False And Me.NoCustomerOption.Value = False Then
    MsgBox "Please ensure you have selected an option for 'Is the customer an existing ASB customer?'", vbExclamation, "Failed to select an option"
    Exit Sub
End If

如果这是两个主要检查,请将整个if块移到此部分之前:

If ProductEnquiryYes.Value = True Then
    Me.MultiPage1.Value = 1
    closeForm = False
    Cells(emptyRow, 20).Value = 1
End If