0

I need to verify that the user entered a CAPITAL letter (A-Z) or a few different punctuation marks (, - ') in an input box. They can only enter one of these characters. Can anyone help me with this?

Thanks in advance!

Something like this:

strtest = InputBox("Enter a capital letter or punctuation mark:")
If strtest <> [A-Z , ' -] Then MsgBox "Invalid Entry."
Brady Mac
  • 15
  • 4
  • what is your current code with `inputbox` in it? I'm asking to provide more accurate suggestion... – Kazimierz Jawor Jun 25 '13 at 18:55
  • I edited my question to include an example of what I am looking for. Thanks! – Brady Mac Jun 25 '13 at 18:57
  • it seems that best option is to use `RegExp` in your situation. There are plenty of examples here in SO. I don't have enough experience with that to help you more but you could start with [this link](http://stackoverflow.com/questions/8146485/returning-a-regex-match-in-vba-excel) – Kazimierz Jawor Jun 25 '13 at 19:15

1 Answers1

0

Like KazJaw, I suspect RegExp is probably the best bet; but I don't have enough experience with it either. So here's another approach:

Function TestEntry(sTest As String) As Boolean

    Dim bTemp As Boolean
    Dim sOKChars As String
    Dim x As Long

    bTemp = False ' by default
    sOKChars = "!@#$%^&*()_+" ' add/remove chars as appropriate

    If Len(sTest) > 1 Then
        MsgBox "Doggone it, I said ONE character only. Try again."
        ' I'd remove the msgbox and test the Len before passing the 
        ' string to this function, actually
    End If

    If 64 < Asc(sTest) And Asc(sTest) < 91 Then
        bTemp = True
    End If

    If Not bTemp Then
        For x = 1 To Len(sOKChars)
            If Mid$(sOKChars, x, 1) = sTest Then
                bTemp = True
                Exit For
            End If
        Next
    End If

    TestEntry = bTemp

End Function

Sub TestTestEntry()
    If TestEntry(",") Then
        MsgBox "Ok"
    Else
        MsgBox "NOT ok"
    End If

End Sub
Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34