iT邦幫忙

0

請教大師:VBA使用UserForm表單輸入資料,若使用動態方式產生TextBox或ComboBox,若要輸入前或後檢查輸入是否正確,如何找到該事件的BeforeUpdate或AfterUpdate?

  • 分享至 

  • xImage

VBA使用UserForm表單輸入資料,若使用動態方式產生TextBox或ComboBox,若要輸入前或後檢查輸入是否正確,如何找到該事件的BeforeUpdate或AfterUpdate....?
手動方式可以找到BeforeUpdate,但是卻無法在動態方式啟動?
請教是否因為程式出問題而導致?請大師指正!程式如下:
` With multiPage.Pages("Page1")
Dim yLabels As Variant
yLabels = Array("受測者", "性別", "測試日期", "執行者", "年齡", "電話", "測試次數", "介紹者") '7
Dim leftOffset As Integer
leftOffset = 120
Dim topOffset As Integer
topOffset = 40
Dim spacing As Integer
spacing = 40

    Dim i As Integer
    Dim j As Integer
    For i = 0 To UBound(yLabels)
            Dim newTextBox As MSForms.TextBox
            Dim newComboBox As MSForms.ComboBox
            Set newTextBox = multiPage.Pages("Page1").Controls.Add("Forms.TextBox.1", , True)
            Set newComboBox = multiPage.Pages("Page1").Controls.Add("Forms.ComboBox.1", , True)

                If i = 1 Or i = 6 Then
                    newComboBox.Font.Name = "微軟正黑體"
                    newComboBox.Font.Size = 12
                    newComboBox.Left = leftOffset + j * spacing
                    newComboBox.Top = topOffset + i * spacing
                    newComboBox.Width = 80
                    newComboBox.Height = 24
                    If i = 1 Then
                        newComboBox.List = Array("男", "女")
                    Else
                        newComboBox.List = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "中測", "後測")
                    End If
                 End If
                 
            newTextBox.Font.Name = "微軟正黑體"
            newTextBox.Font.Size = 12
            newTextBox.Left = leftOffset + j * spacing
            newTextBox.Top = topOffset + i * spacing
            newTextBox.Width = 80
            newTextBox.Height = 24
            Dim yLabelCaption As String
            yLabelCaption = yLabels(i)
            Set newTextBox = multiPage.Pages("Page1").Controls.Add("Forms.TextBox.1", , True)
            newTextBox.Name = "TextBox_" & i                
            TextBoxesDictionary(newTextBox.Name) = newTextBox        
            Set newComboBox = multiPage.Pages("Page1").Controls.Add("Forms.ComboBox.1", , True)
            newComboBox.Name = "ComboBox_" & i 
            ComboBoxesDictionary(newComboBox.Name) = newComboBox                
            Dim yLabel As MSForms.Label
            Set yLabel = multiPage.Pages("Page1").Controls.Add("Forms.Label.1", , True)
            yLabel.Caption = yLabelCaption
            yLabel.Left = leftOffset - 80   
            yLabel.Top = topOffset + i * spacing
            yLabel.Font.Name = "微軟正黑體"
            yLabel.Font.Size = 12
    Next i
End With

'以下為手動方式可以找到,但是卻無動態模式啟動?
Private Sub TextBox_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim currentTextBox As MSForms.TextBox
Set currentTextBox = Me.ActiveControl

Dim textBoxName As String
textBoxName = currentTextBox.Name

If TextBoxesDictionary.Exists(textBoxName) Then
    Dim yLabel As MSForms.Label
    Set yLabel = TextBoxesDictionary(textBoxName & "_Label")
    
    Dim validDate As Boolean
    Dim myDate As Date
    validDate = IsDate(currentTextBox.Text)
    
    If validDate Then
        myDate = CDate(currentTextBox.Text)
        If myDate > Date Then
            MsgBox "日期不能大於今天。", vbExclamation, "錯誤"
            currentTextBox.SelStart = 0
            currentTextBox.SelLength = Len(currentTextBox.Text)
            yLabel.ForeColor = RGB(255, 0, 0)
            Cancel = True
        Else
            yLabel.ForeColor = RGB(0, 0, 0)
        End If
    Else
        MsgBox "請輸入有效的日期格式Year(/or-)Month(/or-)Day。", vbExclamation, "錯誤"
        currentTextBox.SelStart = 0
        currentTextBox.SelLength = Len(currentTextBox.Text)
        yLabel.ForeColor = RGB(255, 0, 0)
        Cancel = True
    End If
End If

End Sub

`

froce iT邦大師 1 級 ‧ 2023-08-24 16:10:22 檢舉
https://stackoverflow.com/questions/3014421/how-to-add-events-to-controls-created-at-runtime-in-excel-with-vba

code要記得放在form裡。
建議參考最後一篇回答比較容易看懂,重點是下面這句:

Public WithEvents CustomBtn As MSForms.CommandButton
lin520 iT邦新手 4 級 ‧ 2023-08-26 21:02:00 檢舉
先謝謝大師,我試試!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答