验证:只允许一个特定的单词在VBA中输入一次Excel
我有4个不同的单词(financial, location, course, professor
),可以在文本框中输入,但每个单词只能在文本框的每个输入中使用一次。验证:只允许一个特定的单词在VBA中输入一次Excel
例如,我在文本框中输入了一个如下所示的句子:“我的财务存在问题,因为我的家人正面临财务问题”,下面的代码将此句子处理为拆分文本。
我想为验证所要做的就是通知用户(可能通过MSGBOX)是这样的:“错误 - 你必须在一个句子中使用的财务只有一次”
另外,如果课程,位置和教授在一个句子中多次使用,还应该给出一个msgbox。
Private Sub CommandButton1_Click()
Call SplitText
End Sub
Sub SplitText()
Dim WArray As Variant
Dim TextString As String
TextString = TextBox1
WArray = Split(TextBox1, " ")
If (TextString = "") Then
MsgBox ("Error: Pls Enter your data")
Else
With Sheets("DatabaseStorage")
.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(UBound(WArray) + IIf(LBound(WArray) = 0, 1, 0)) = Application.Transpose(WArray)
End With
MsgBox ("Successfully inserted")
End If
End Sub
试试这个:
Private Sub CommandButton1_Click()
Call SplitText
End Sub
Sub SplitText()
Dim sentence As String
Dim mycount As Long
sentence = InputBox("Enter the sentence")
mycount = UBound(Split(sentence, "financial"))
If mycount > 1 then
Msgbox "Error - you must used financial only once in a sentence"
End if
'Here the rest of the code you need
End Sub
希望它能帮助。
嗨,先生,它的工作原理msgbox,但每次我尝试保存到我的数据库它返回错误:/ –
@RaeIan记住你在代码中使用了一个名为WArray的变量,该变量不在我的,因为我没有使用你的分割方法,它只是当你根据你需要保存的时候将其替换掉。 – jsanchezs
这不是一个代码snippit,不要像这样格式化它。 VBA不会被困在一个小窍门里面。 – Luuklag
好的复制先生我将编辑我的帖子 –
当人们在文本框中输入财务两次时,您会遇到什么问题?你可以很容易地检查这个词是否出现在他们的条目中,它出现的次数应该不重要。 – Luuklag