我写了三个不同的Private Sub函数,所有这些函数都做同样的事情Private Sub txt_noRooms_KeyPress和Private Sub txt_length_KeyPress确保用户只能在引用的文本字段中输入值 1 到 9 而Private Sub txt_studentNo_KeyPress允许用户输入值 0 到 9 到引用的文本字段中。
有什么办法可以合并这三个函数并让它们仍然支持引用的单元格并保持相同的条件?我想这样做是为了使代码更有效率。
Private Sub txt_noRooms_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
' Restrict entry to txt_noRooms
Select Case KeyAscii
Case Asc("1") To Asc("9")
Case Asc("-")
If InStr(1, Me.txt_noRooms.Text, "-") > 0 Or Me.txt_noRooms.SelStart > 0 Then
KeyAscii = 0
End If
Case Asc(".")
If InStr(1, Me.txt_noRooms.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub txt_length_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
' Restrict entry to txt_length
Select Case KeyAscii
Case Asc("1") To Asc("9")
Case Asc("-")
If InStr(1, Me.txt_length.Text, "-") > 0 Or Me.txt_length.SelStart > 0 Then
KeyAscii = 0
End If
Case Asc(".")
If InStr(1, Me.txt_length.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub txt_studentNo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
' Restrict entry to txt_studentNo
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc("-")
If InStr(1, Me.txt_studentNo.Text, "-") > 0 Or Me.txt_studentNo.SelStart > 0 Then
KeyAscii = 0
End If
Case Asc(".")
If InStr(1, Me.txt_studentNo.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub