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?

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Meloun
  • 13,601
  • 17
  • 64
  • 93
  • 2
    [Here](https://stackoverflow.com/a/55449103/7558682) 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. – Damian Apr 01 '19 at 10:47

2 Answers2

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.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
2

Remove the quotes:

If varInput = False Then
iDevlop
  • 24,841
  • 11
  • 90
  • 149
  • 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 Apr 01 '19 at 11:01
  • @Meloun Explicitly declare `varInput` as *Variant* rather than *String*. – Gary's Student Apr 01 '19 at 11:38
  • 1
    @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. – Pᴇʜ Apr 01 '19 at 11:43