我有一個當前有 15 個組合框的用戶表單。允許用戶選擇本季度、上季度和去年的 5 份報告。由于有很多框可供選擇,我認為應用一些錯誤處理以避免丟失任何框會很好。
我的代碼是這樣的(顯然每個框都重復):
If IsNull(UF_RM_Reports.Report1.Value) Then
MsgBoxResult = MsgBox("No report selected for Report 1 current quarter, is this correct?", vbYesNo vbQuestion, "Report Template")
If MsgBoxResult = vbNo Then
End
Else
End If
End If
我遇到的問題是,如果用戶意識到他們沒有選擇報告并按“否,我有結束”,這不僅會結束宏,還會關閉組合框。如果您重新打開它,則所有選擇都將消失。有點挫敗錯誤處理的意義,因為他們需要重新開始。
我想知道是否有什么我可以改變 End 停止代碼但允許用戶回傳并選擇丟失的報告。
提前致謝
uj5u.com熱心網友回復:
如果要求是讓用戶注意他/她錯過了報告選擇這一事實,為什么不嘗試在其中任何一個事件中擺弄組合框的顏色屬性——比如?
在 useform activate 事件中設定組合框的顏色狀態
Private Sub UserForm_Activate() ComboBox1.BackColor = vbRed UserForm1.ComboBox1.AddItem "A" UserForm1.ComboBox1.AddItem "B" UserForm1.ComboBox1.AddItem "C" End Sub然后在組合框更改事件中將顏色改回原來的顏色
Private Sub ComboBox1_Change() If ComboBox1.value = "" Then ComboBox1.BackColor = vbRed Else ComboBox1.BackColor = &H80000005 End If End Sub
uj5u.com熱心網友回復:
有幾種方法可以處理這種情況。由于我不知道您的用戶表單的詳細資訊,因此我將概述一般方法。我傾向于在按下 UserForm 上的按鈕時驗證控制元件,這表明用戶已準備好生成報告:
Private Sub CommandButton1_Click()
If Trim(Report1.Value) = "" Then
If MsgBox("No report selected for Report 1...", vbYesNo vbQuestion, "Report Template") = vbNo Then
Report1.SetFocus
Exit Sub
End If
End If
If Trim(Report2.Value) = "" Then
If MsgBox("No report selected for Report 2...", vbYesNo vbQuestion, "Report Template") = vbNo Then
Report2.SetFocus
Exit Sub
End If
End If
'create the reports when nothing has been missed or
'the user wants to proceed anyway
MsgBox "create reports"
End Sub
因為有很多控制元件,你可以用一個回圈來完成同樣的事情:
Private Sub CommandButton1_Click()
Dim c As Control
For Each c In Me.Controls
If TypeOf c Is ComboBox Then
If Trim(c.Value) = "" Then
If MsgBox("You have not selected all reports. Is this correct?", vbYesNo vbQuestion, "Report Template") = vbNo Then
Report1.SetFocus
Exit Sub
Else
Exit For
End If
End If
End If
Next
'create the reports when nothing has been missed or
'the user wants to proceed anyway
MsgBox "create reports"
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/383988.html
上一篇:不放置零
