所以我试着确保所有的TextBoxes都填好了。但是,我只想让这个脚本在Excel UserForm上检查MultiPage页面上的某个页面。下面是我使用的代码,但它检查用户表单中的每个TextBox,而不是特定页面上的那些。还有一张图片给你看。我圈出了名为"Box“的页面,这也是我想在验证TextBoxes和要验证的TextBoxes时引用的第3页。
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发布于 2019-07-22 11:12:39
您需要引用MultiPage对象的Pages集合。因此,假设多页控件名为MultiPage1,您需要修改该行,如下所示...
For Each Ctrl In Me.MultiPage1.Pages(2).Controls注意,Pages集合的索引是从0开始的。
https://stackoverflow.com/questions/57138570
复制相似问题