Frage

I really searched hours and hours, but I can't find any solutions.

You should only enter numbers into the Inputbox and a msgbox should sppears when you just hit ok without any number or string...

The first part was easy, but I always get an error message by just hitting OK!

Public Sub test()
Dim vntReturn As Variant
vntReturn = Application.InputBox("Bitte Wert eingeben", "Eingabe", , , , , , 1)
If StrPtr(vntReturn) = 0 Then
    MsgBox "Abbrechen gedrückt"
Else
    If vntReturn = False Then
        MsgBox "Nix eingegeben"
    Else
        MsgBox vntReturn
    End If
End If
End Sub
War es hilfreich?

Lösung

This is happening because you're declaring the Type for this InputBox to a number. So excel will automatically try to correct this. You can use an InputBox without a Type and program your own verification for checking if it's an integer or not.

Otherwise you can also add this before your code:

Application.DisplayAlerts = False

And then set it to True after. Now when you hit ok you won't be prompted with the error, but the InputBox will not go away. You could add additional instructions to the InputBox to make it clear it needs a number.

Andere Tipps

@mehow: as Alex D just said: Your answers are similar ;-) First I used the code of mehow, but now I just create a userform with only an "OK-button".

Private Sub Rechnen_Click()
Dim i As Integer            ' Variable deklarieren
Dim Sum As Integer
Dim counter As Variant
i = 0                       ' deklariert, löst beim Kompilieren keinen Fehler aus
Sum = 0                     ' nicht deklarierte Variable löst beim Kompilieren einen Fehler aus
counter = TextBox1.Value
Application.DisplayAlerts = False
If Not IsNumeric(counter) Then
Exit Sub
Else
Unload Userform1
    On Error Resume Next
    Do Until i >= counter

    Zahl = InputBox("Pls enter a number:", i + 1 & ". Versuch")
    Sum = Sum + Zahl
    i = i + 1

    If Not IsNumeric(Zahl) Then
    MsgBox "calculation premature cancelled!"
    Exit Do
    End If

    Loop
    Ausgabe = MsgBox("Die Summe lautet: " & Sum, vbInformation, "Ergebnis")
    Question = MsgBox("is it enough?", vbYesNo + vbQuestion, "repeat")
        If Question = vbNo Then
        Userform1.Show
        Else
        Unload Userform1
        Exit Sub
        End If
End If
End Sub

and so the userform looks like:

enter image description here

Now the program works fine ;) Thank you guys!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top