validation:只允许一个特定的单词一次可以在VBA Excel的文本框中input

我有一个可以在文本框中input的4个不同的单词( financial, location, course, professor ),但是每个单词只能在文本框的每个input中使用一次。

例如,我在文本框中input如下的句子:“我的财务问题,因为我的家庭正面临财务问题”,下面的代码将这个句子处理成分割文本。

我想要做的validation是通知用户(也许通过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 

希望能帮助到你。