【问题标题】:Validation: Allowing one specific word only once can be input in textbox in VBA Excel验证:仅允许在 VBA Excel 的文本框中输入一个特定单词一次
【发布时间】:2017-09-05 23:29:46
【问题描述】:

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

例如,我在文本框中输入这样的句子:“我有财务问题,因为我的家人面临财务问题”下面的代码将这句话处理为拆分文本。

我想要做的验证是通知用户(可能通过 msgbox):

“错误 - 你必须在一个句子中只使用一次金融。”

另外,如果在一个句子中多次使用 course、location 和professor 也应该给出一个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

【问题讨论】:

  • 这不是代码片段,不要这样格式化。 VBA 不能在代码段内运行。
  • 好的,先生,我会编辑我的帖子
  • 当人们在文本框中输入两次财务时,您会遇到什么问题?您可以轻松地检查该词是否出现在他们的条目中,它出现的次数并不重要。
  • 因为如果人们输入金融两次,它也将被计算在内,因为拆分文本的目的是获取我想要获取的单词并将其计数为不同的,这意味着我得到了计数根据计数功能显示出现次数最多的单词,但是如果用户在一个句子中输入了许多金融,那么就有可能计数不准确。
  • 我认为如果您在将数组发布到数据库之前简单地从数组中删除重复项,会更加用户友好。你可以看看这里:stackoverflow.com/questions/11870095/…

标签: vba excel


【解决方案1】:

试试这个:

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 的变量,该变量不在我的变量中,因为我没有使用您的拆分方法,只是您在保存时根据您的内容替换它需要那里。
猜你喜欢
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 2014-10-12
相关资源
最近更新 更多