Excel VBA输入框仅限字母和最大长度

时间:2017-01-16 13:52:01

标签: excel vba excel-vba

我想要一个输入框,只允许1-3个字符,只有A-Z字母。我尝试了几种不同的方法,但似乎无法开始工作

我现在正在使用此代码,但它只限制长度

{{1}}

5 个答案:

答案 0 :(得分:1)

一种方式:

Sub Change_Ticket_Initials()

    Dim strReturn As String

    Do
        strReturn = UCase$(InputBox("Enter Initials", "Change Ticket Initials"))

        Select Case True
            Case strReturn = ""
                 Exit Sub
            Case strReturn Like "[A-Z]", _
                 strReturn Like "[A-Z][A-Z]", _
                 strReturn Like "[A-Z][A-Z][A-Z]"

                 Control_Sheet_VB.Range("C2").Value = strReturn
                 Exit Do
            Case Else
                 MsgBox "Must Be 1-3 Characters Try Again"
        End Select
    Loop

End Sub

答案 1 :(得分:1)

我认为这正是他们提出ng-repeat='item in items | filter: hideNullRatings' 对象的原因。

您可以通过输入RegEx来检查A-Z(或a-z)字符 。 您可以通过添加RegEx.Pattern = "[a-zA-Z]"将其展开为允许1-3个字符的大小。

感谢@Slai,需要在开头和结尾添加.Pattern = "[a-zA-Z]{1,3}"^以关闭它。

<强>代码

$

答案 2 :(得分:0)

你可能在此之后

Option Explicit

Sub Change_Ticket_Initials()
    Dim strReturn As String
    Dim Control_Sheet_VB As Worksheet

    Do
        strReturn = UCase(Application.InputBox("Enter Initials", "Change Ticket Initials", Type:=2))
        If strReturn = vbNullString Then Exit Sub 'if "" exit sub
        If strReturn Like "[A-Z]" Or strReturn Like "[A-Z][A-Z]" Or strReturn Like "[A-Z][A-Z][A-Z]" Then Exit Do
        MsgBox "Must Be 1-3 Characters" & vbCrLf & vbCrLf & "Try Again"
    Loop
    Control_Sheet_VB.Range("C2").Value = strReturn
End Sub

答案 3 :(得分:0)

您可以使用以下功能轻松检查字符串是否仅包含字母

Public Function CheckIfAlpha(strValue As String) As Boolean
    CheckIfAlpha = strValue Like WorksheetFunction.Rept("[A-ZA-Z]", Len(strValue))
End Function 

整个实施就像你的if条件一样:

Sub Change_Ticket_Initials()

Dim strReturn As String
strReturn = InputBox("Enter Initials", "Change Ticket Initials")

    If strReturn = vbNullString Then Exit Sub 'if "" exit sub

If Len(strReturn) < 1 Or Len(strReturn) > 3 Or Not CheckIfAlpha(strReturn) Then
MsgBox "Must Be 1-3 Alphabet Characters Try Again"
Run "Change_Ticket_Initials"

    Else
Control_Sheet_VB.Range("C2").Value = UCase(strReturn)
    End If

End Sub
Public Function CheckIfAlpha(strValue As String) As Boolean
    CheckIfAlpha = strValue Like WorksheetFunction.Rept("[A-ZA-Z]", Len(strValue))
End Function

PS:经过试验和测试的代码:)

答案 4 :(得分:0)

我喜欢这段代码,它检查输入中是否有数字,你可以尝试一下:

Private Sub Change_Ticket_Initials()

Dim strReturn As String
Dim strCheck as Boolean
Dim aChar As Boolean

strReturn = InputBox("Enter Initials", "Change Ticket Initials")

For strCheck = 1 To Len(strReturn)
    aChar = False
    aChar = IsNumeric(Mid(strReturn, strCheck, 1))

    If aChar = True Then
        MsgBox "No numbers allowed"
        Exit Sub
    End If

Next strCheck

If strReturn = vbNullString Then Exit Sub 'if "" exit sub

If Len(strReturn) < 1 Or Len(strReturn) > 3 Then
    MsgBox "Must Be 1-3 Characters Try Again"
    Run "Change_Ticket_Initials"

Else
    Control_Sheet_VB.Range("C2").Value = UCase(strReturn)
End If

End Sub