因此,我正在努力确保所有内容TextBoxes
都已填写。但是,我只希望此脚本检查MultiPage
Excel页面上的某个页面UserForm
。下面是我正在使用的代码,但它正在检查TextBox
用户表单中的每一个,而不是特定页面上的。还有一张图给大家看看。我圈出了名为“Box”的页面,它也是第 3 页,我想在验证TextBoxes
和验证时参考TextBoxes
它。
Private Sub CommandButton2_Click()
Dim Ctrl As Control
Dim Answer1 As VbMsgBoxResult
' Checks to make sure data has been entered into all fields on the UserForm Page
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.TextBox Then
If Ctrl.Value = vbNullString Then
MsgBox "All fields must be completed before the information can be updated.", vbOkay + vbExclamation, "Missing Information"
Exit Sub
End If
End If
Next
' Displays box with Yes or No asking if they are sure they want to save and close document
Answer1 = MsgBox("Doing this will overwrite the previous data that was entered. Are you sure you want to do this?", vbYesNo + vbExclamation, "Update Cabinet Information")
' Writes the new data entered into the workbook
If Answer1 = vbYes Then
Sheet5.Range("B1").Value = TextBox1.Value
If IsDate(TextBox2.Value) Then
Sheet5.Range("B2").Value = Format(TextBox2.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("B3").Value = TextBox3.Value
Sheet5.Range("G1").Value = TextBox4.Value
If IsDate(TextBox5.Value) Then
Sheet5.Range("G2").Value = Format(TextBox5.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("G3").Value = TextBox6.Value
Sheet5.Range("J1").Value = TextBox7.Value
If IsDate(TextBox8.Value) Then
Sheet5.Range("J2").Value = Format(TextBox8.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("J3").Value = TextBox9.Value
Sheet5.Range("M1").Value = TextBox10.Value
If IsDate(TextBox11.Value) Then
Sheet5.Range("M2").Value = Format(TextBox11.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("M3").Value = TextBox12.Value
Sheet5.Range("P1").Value = TextBox13.Value
If IsDate(TextBox14.Value) Then
Sheet5.Range("P2").Value = Format(TextBox14.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("P3").Value = TextBox15.Value
Sheet5.Range("S1").Value = TextBox16.Value
If IsDate(TextBox17.Value) Then
Sheet5.Range("S2").Value = Format(TextBox17.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("S3").Value = TextBox18.Value
End If
End Sub
您需要引用 MultiPage 对象的 Pages 集合。因此,假设多页控件名为 MultiPage1,您需要按如下方式修改该行...
For Each Ctrl In Me.MultiPage1.Pages(2).Controls
请注意,Pages 集合的索引是从 0 开始的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句