验证用户窗体(Excel VBA)上的文本框条目 [英] Validating textbox entry on Userform (Excel VBA)
问题描述
我有一个文本框(在本例中为textbox11),前端用户将需要以某种形式输入半天".
I have a textbox (in this instance it is textbox11) where the front end user will need to input "Half Day" in some form or another.
这是我的代码:
Private Sub TextBox11_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Me.TextBox11 = "half" Or Me.TextBox11 = "half day" Or Me.TextBox11 = "half-day" Or Me.TextBox11 = 0.5 Or Me.TextBox11 = "1/2" Then
Me.TextBox11 = "Half Day"
End If
End Sub
当我运行它时,它可以很好地工作-在我设定的条件下.但是,如果用户说输入"hALf dAy",则该值将不被验证并固定为标准格式,因为它不在我的条件列表中.
When I run it, it works wonderfully - for the conditions I have set. However, if a user, say, inputs "hALf dAy", then this will not be validated and fixed to standard form as it is not on my condition list.
我永远无法在其中放置用户可能输入的所有排列.有没有办法使此不区分大小写?因此,无论用户输入哪种情况,它都将根据我的条件进行检查.
There is noway where I could put all permutations of what a user might put in. Is there a way to make this case-insensitive? So no matter what case a user puts in, it will check against my conditions anyway.
谢谢
推荐答案
LCase
(或 UCase
).通过将所有字符都转换为小写,然后与小写字符串进行比较,您实际上是不区分大小写的.这是一个例子.
LCase
(or UCase
) the user input. By converting all characters to lower case, and then comparing to your lower-case strings, you are effectively case-insensitive. Here's an example of that.
Private Sub TextBox11_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Select Case Trim(LCase(Me.TextBox11.Text))
Case "half", "half day", "half-day", "0.5", ".5", "1/2"
Me.TextBox11.Text = "Half Day"
Case Else
' do nothing?
'
'
End Select
但是,如果您真的想验证,最好的选择是使用具有预定义值的 ComboBox
,而不是用户可以在其中键入任何垃圾的文本框.
But if you really want validation the best option is to use a ComboBox
with predefined values rather than a text box where the user can type any ol' garbage.
这篇关于验证用户窗体(Excel VBA)上的文本框条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!