3
varInput = Application.InputBox("Text", "Title", Type:=2)
If varInput = "False" Then
    Exit Sub
End If

If the cancel button is pressed, the return value is a string with "False". But on some computer with german setting I will get "Falsch" !

How should be this handled?

1
  • 2
    Here you can read how to from @Pᴇʜ. The question has nothing in common with yours, but part of the code handles this. See if it helps. Commented Apr 1, 2019 at 10:47

2 Answers 2

4

You must always also test the type of the variable to be boolean: VarType(varInput) = vbBoolean when using the Application.InputBox.

Dim varInput As Variant
varInput = Application.InputBox("Text", "Title", Type:=2)

If VarType(varInput) = vbBoolean And varInput = False Then
    Exit Sub
End If

If you test only for False

If varInput = False Then

… then it will also exit sub if you enter 0 or Falsch (in german Excel) or False (in English Excel).

This hapens because a string of 0 or "Falsch" (or "False") automatically casts into a boolean if compared with False. But only the Cancel button returns a true boolean.

Sign up to request clarification or add additional context in comments.

1 Comment

Very nice approach.
2

Remove the quotes:

If varInput = False Then

3 Comments

In that case it works if I press cancel, but it fails if I write something and press OK. The varIpunt is than string containg e.g. "test" and it fails on the modified line.
@Meloun Explicitly declare varInput as Variant rather than String.
@patrick you must also test for VarType(varInput) = vbBoolean otherwise it will exit sub also on values like 0 or Falsch (in OP's case). See my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.