1

これは、Excel UseForm 内のテキスト ボックスの特定の検証に関する以前の質問の続きです。Sorceriはこのコードを取得するのに大いに役立ちましたが、今は微調整が必​​要です。テキストボックスごとに異なる検証を行う必要があります。「ツールボックス」の「コントロール」からテキストボックスを作成しました。彼らはそのように順番に名前を付けました。「info1」、「info2」、「info3」など。

ほとんどのテキストボックスの検証は以下で確認できます (「1-5」、「88」、および「99」)。ただし、2 つのテキスト ボックス ("1-3" と "88") は異なる方法で検証する必要があります。これに加えて、テキスト ボックスの 3 番目のグループ ("0-10" および "88") には、3 番目の検証セットが必要です。私は VBA とプログラミング全般にかなり慣れていませんが、すぐに習得できます。どんな助けでも大歓迎です。

明確にするために、ソルセリの括弧内の横に私自身の解説を追加しました. 改めまして、皆様。

Private Sub cmdEnter_Click()
Dim ctl As Control
For Each ctl In Me.Controls
    'check to see if it is a textbox (I'm guessing I could specify which textboxes here, but I'm unsure)
    If TypeOf ctl Is MSForms.TextBox Then
        Dim tBox As MSForms.TextBox
        Set tBox = ctl
        'we have a textbox so validate the entry (Again, I'm hoping to make this into, "We found textbox variant A and so will validate its entry with validation A, etc)
        If validateTextBox(tBox) Then
            'did not validate so set focus on the control
            MsgBox "Invalid Entry!", vbCritical, "Invalid!"
            ctl.SetFocus
            'release the object
            Set tBox = Nothing
            Exit Sub
        End If
        Set tBox = Nothing
    End If
Next
End Sub




'validate a textbox's value and return true or false
Private Function validateTextBox(tb As MSForms.TextBox) As Boolean
    Dim sValue As String
    Dim bInvalid As Boolean
    bInvalid = True
    sValue = Trim(tb.Text)
    If sValue = "1" Or sValue = "2" Or sValue = "3" Or sValue = "4" Or sValue = "5" Or sValue = "99" Or sValue = "88" Then
        bInvalid = False
    End If
    'return the results
    validateTextBox = bInvalid
End Function
4

3 に答える 3

0

したがって、本当の問題は、テキストボックス要素がどのようにグループ化されるかです。検証が必要な3つの異なるグループを区別する方法が必要です。最も簡単な方法は、各テキストボックスの名前に現在の名前を追加する命名規則です(グループ化コントロールのIEフレームを使用できる場合を除く)。ABCまたは実際には他の任意の命名スキームを使用できます。この例では、ABCを使用します。

グループAは検証します(「1-5」、「88」、および「99」)

グループBは検証します(「1-3」および「88」)

グループCは検証します(「0-10」および「88」)

各テキストボックスの最後に、テキストボックスが属するグループを区別するためにA、B、またはCを追加します。テキストボックス要素は、「info1」、「info2」、「info3」、

したがって、「info1A」、「info2A」、「info3C」、「info4B」、「info5C」....「info30A」、

これで、各グループの列挙型を追加します

Private Enum ValidationGroup
NONE
A 
B
C
End Enum

'グループを取得するためのメソッドにswitchステートメントを追加します

Private Sub cmdEnter_Click()
Dim ctl As Control
For Each ctl In Me.Controls
    'check to see if it is a textbox
    If TypeOf ctl Is MSForms.TextBox Then
        Dim tBox As MSForms.TextBox
        Dim tbGroup As ValidationGroup

        Set tBox = ctl
        'we have a textbox so check the group and validate the entry
        'get the group to validate against
        tbGroup = Switch(Right(tBox.Name, 1) = "A", ValidationGroup.A, _
        Right(tBox.Name, 1) = "B", ValidationGroup.B, _
        Right(tBox.Name, 1) = "C", ValidationGroup.C, _
        Right(tBox.Name, 1) <> "*", ValidationGroup.NONE) 'default value of none

        If validateTextBox(tBox, tbGroup ) Then
            'did not validate so set focus on the control
            MsgBox "Invalid Entry!", vbCritical, "Invalid!"
            ctl.SetFocus
            'release the object
            Set tBox = Nothing
            Exit Sub
        End If
        Set tBox = Nothing 
    End If
Next
End Sub

列挙型を含めるようにvalidateTextBoxを変更します

**補足として、この方法はより効率的になるようにオーバーホールされる可能性があります

'validate a textbox's value and return true or false
Private Function validateTextBox(tb As MSForms.TextBox, vg As ValidationGroup) As Boolean
    Dim sValue As String
    Dim bInvalid As Boolean
    bInvalid = True
    sValue = Trim(tb.Text)t
    'check the validation group then check the values
    If vg = ValidationGroup.A Then
        If sValue = "1" Or sValue = "2" Or sValue = "3" Or sValue = "4" Or sValue = "5" Or sValue = "99" Or sValue = "88" Then
            bInvalid = False
        End If
    ElseIf vg = ValidationGroup.B Then
        If sValue = "1" Or sValue = "2" Or sValue = "3" Or sValue = "88" Then
            bInvalid = False
        End If
    ElseIf vg = ValidationGroup.C Then
        If sValue = "0" Or sValue = "1" Or sValue = "2" Or sValue = "3" Or sValue = "4" Or sValue = "5" Or _
        sValue = "6" Or sValue = "7" Or sValue = "8" Or sValue = "9" Or sValue = "10" Or sValue = "88" Then
            bInvalid = False
        End If
    End If
    'return the results
    validateTextBox = bInvalid
End Function
于 2013-02-16T03:44:50.127 に答える
0

以下を試してください。

テキストボックスの名前に基づいて、関数にいくつかの選択ケースを配置しました。

簡単にするために、検証グループに基づいてテキスト ボックスに (再) 名前を付けると仮定しました。ただし、個々のテキストボックス名をステートメントに入れ、ステートメント内の関数Case Is =を取り除くことができます。LeftSelect Case

また、If ステートメントを読み書きしやすくするために to をtb.Value変更しました。Integer

これは、答えを構築するための明確な構成を与えることと同じくらい、完全な証明を意図したものではありません。

Function validateTextBox(tb As MSForms.TextBox) As Boolean

    Dim iValue As Integer, sName As String, bInvalid As Boolean

    bInvalid = True

    sName = tb.Name
    iValue = CInt(Trim(tb.Text))

    If iValue = 88 Then 'because this is common among all the groups check for it first

        bInvalid = False

    Else

        Select Case Left(sName, "4")

            Case Is = "grp1"

                If (iValue > 1 And iValue <= 5) Or iValue = 99 Then bInvalid = False

            Case Is = "grp2"

                If (iValue > 1 And iValue <= 3) Then bInvalid = False

            Case Is = "grp3"

                If (iValue > 1 And iValue <= 10) Then bInvalid = False

        End Select

    End If

    'return the results
    validateTextBox = bInvalid

End Function
于 2013-02-14T17:20:08.160 に答える
0

Tagこの「1-30,88,89」のように、各テキストボックスのプロパティに検証値を入れます(引用符はありません)。これらの変数宣言も少し整理したい場合があります。

Function ValidateTextBox(tb As MSForms.TextBox) As Boolean
    Dim sTag, i, arr, arrVal, min, max, v, tbVal, ok As Boolean

    tbVal = Trim(tb.Value)

    If IsNumeric(tbVal) Then

        sTag = Replace(tb.Tag, " ", "") 'remove any spaces from tb Tag

        v = CInt(tbVal) 'convert the textbox value to integer

        If sTag <> "" Then
            arr = Split(sTag, ",")
            For i = LBound(arr) To UBound(arr)
                arrVal = Trim(arr(i))
                If InStr(arrVal, "-") > 0 Then
                    'check against a range
                    min = CInt(Split(arrVal, "-")(0))
                    max = CInt(Split(arrVal, "-")(1))
                    If v >= min And v <= max Then
                        ok = True
                        Exit For
                    End If
                Else
                   'just a single value to check against
                   If v = CInt(arr(i)) Then
                        ok = True
                        Exit For
                   End If
                End If
            Next i
        Else
            MsgBox "No rule for this textbox: " & tb.Name
        End If

    End If

    ValidateTextBox = ok

End Function
于 2013-02-14T17:28:03.040 に答える