Share via

Different error message for an Application.Inputbox message

Anonymous
2022-11-21T05:07:35+00:00

I'm trying to make an Application.Inputbox which could result on 3 errors:

-The input is empty

-The path does not exsist

-No error

The problem is, when I clic cancel in the InputBox, it goes to Erreur1. What I'm trying to do is when cancel clicked the program to stop

My code:

Direction = Application.InputBox("Quelle est la direction du fichier?", "Nomage", default, Type:=2)

If Len(Direction) <= 0 Then GoTo Erreur1:                          'The input is empty

If Len(Dir(Direction, vbDirectory)) <= 0 Then GoTo Erreur2:        'The path does not exist

Erreur1:
If MsgBox("Veuillez mêtre une addresse dans la fenêtre!", vbCritical, "Erreur") Then
End
Else
End
End If
Stop

Erreur2:
If MsgBox("Le dossier n'existe pas", vbCritical, "Erreur") = vbOK Then
Else
End If
Stop
Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Rory Archibald 18,965 Reputation points Volunteer Moderator
2022-11-21T11:32:43+00:00

You can use Inputbox rather than Application.Inputbox and test the result with StrPtr:

Direction = InputBox("Quelle est la direction du fichier?", "Nomage", Default) 

If StrPtr(Direction) = 0 Then Exit Sub 

If Len(Direction) <= 0 Then GoTo Erreur1:                          'The input is empty 

If Len(Dir(Direction, vbDirectory)) <= 0 Then GoTo Erreur2:        'The path does not exist 

Erreur1: 

If MsgBox("Veuillez mêtre une addresse dans la fenêtre!", vbCritical, "Erreur") Then 

End 

Else 

End 

End If 

Stop 

Erreur2: 

If MsgBox("Le dossier n'existe pas", vbCritical, "Erreur") = vbOK Then 

Else 

End If 

Stop

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2022-11-25T10:56:09+00:00

    It perfectly works thanks!!!

    Was this answer helpful?

    0 comments No comments
  2. OssieMac 48,001 Reputation points Volunteer Moderator
    2022-11-21T11:00:54+00:00

    Thanjs but it doesn't work 😅​
    The error message must be displayed when ok is clicked and the InputBox is empty. But not when cancel is clicked.

    I did not interpret that was what you wanted in your original question. Try the following example and see if it helps. Note the Title:= and Default:= specifiers in the InputBox command.

        Dim Direction As Variant        'Dimension as variant so will accept any type of data 
    
        Direction = Application.InputBox("Quelle est la direction du fichier?", Title:="Nomage", Default:=0, Type:=2) 
    
        Select Case Direction 
    
            Case False  'If user clicks Cancel 
    
                MsgBox "User cancelled" 
    
                Stop    'For testing purposes only. Replace with required code 
    
            Case ""     'If blank 
    
                MsgBox "User entered blank" 
    
                Stop    'For testing purposes only. Replace with required code 
    
            Case Else   'Any other entry 
    
                MsgBox "User did not Cancel but entered a value and clicked OK" 
    
                Stop    'For testing purposes only. Replace with required code 
    
        End Select
    

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-11-21T08:49:08+00:00

    Thanjs but it doesn't work 😅​
    The error message must be displayed when ok is clicked and the InputBox is empty. But not when cancel is clicked.

    Was this answer helpful?

    0 comments No comments
  4. OssieMac 48,001 Reputation points Volunteer Moderator
    2022-11-21T06:48:00+00:00

    Test for cancel as follows

    Dim Direction As Variant        'Dimension as variant so will accept boolean result when Cancel clicked
    
    Direction = Application.InputBox("Quelle est la direction du fichier?", "Nomage", Default, Type:=2) 
    
    If Direction = False Then GoTo Erreur1
    

    Was this answer helpful?

    0 comments No comments