für alle Lebenslagen...,
26.02.2009 11:14:32
Tino
Hallo,
hier mal ein Aufbau mit Beispielen.
Option Explicit
Private Function InStrNumperAndLen(strString$, Optional MaxLen As Long = -1) As Boolean
Dim Regex As Object
Dim sKomma As String, strStringTemp As String
sKomma = IIf("0.5" * 2 = 1, ".", ",") 'Ländereinstellung prüfen
strStringTemp = Replace(strString, sKomma, "")
Set Regex = CreateObject("Vbscript.Regexp")
With Regex
.Pattern = "\D"
.Global = True
InStrNumperAndLen = Not _
.test(strStringTemp) And _
IIf(MaxLen = -1, True, (Len(strString) = MaxLen)) And _
(Len(strStringTemp) >= Len(strString) - 1)
End With
Set Regex = Nothing
End Function
Sub Beispiele()
Dim strText As String
'Beispiel 1 nur Text zulassen*****************************
strText = "Hallo" 'Text InputBox
If InStrNumperAndLen(strText) Then
MsgBox "Die eingabe ist eine Zahl"
Else
MsgBox "Die eingabe ist ein Text"
End If
'_________________________________________________________
'Beispiel 1.1 nur Text zulassen (auch mit Dezimalstellen)*
strText = "15,5" 'Text InputBox
If InStrNumperAndLen(strText) Then
MsgBox "Die eingabe ist eine Zahl"
Else
MsgBox "Die eingabe ist ein Text"
End If
'__________________________________________________________
'Beispiel 2 nur Zahlen zulassen****************************
strText = "12" 'Text InputBox
If InStrNumperAndLen(strText) Then
MsgBox "Die eingabe ist eine Zahl"
Else
MsgBox "Die eingabe ist ein Text"
End If
'__________________________________________________________
'Beispiel 3 nur Zahlen zulassen mit betimmter länge********
strText = "12" 'Text InputBox
If InStrNumperAndLen(strText, 2) Then
MsgBox "Die eingabe ist eine Zahl mit richtiger Länge"
Else
MsgBox "Die eingabe ist ein Text oder Länge ist falsch"
End If
'__________________________________________________________
End Sub
Habe ich etwas vergessen?
Gruß Tino