Missing something with Do/Loop and If/else/End if

compta

New Member
Joined
Jun 22, 2004
Messages
24
Hi,

I have wrote a code with Do...Loop, in between, i have used If..else..End if structure. But It seems as though the "Else" does not work in that case.
This is the code:
Code:
Private Sub cmdOK_Click()
 
'******************************************
'******************************************
'Code de connexion dans l'applicatif
 
'******************************************
'******************************************
 
Dim sLogin As String
Dim sPwd As String
Dim i As Integer
Dim iVarTest As Integer
Dim iLongueur As Integer
 
'Gestionnaire des erreurs
On Error Resume Next
 
'Récupération des paramètres de connexion
sLogin = txtNom
sPwd = txtMot
 
'Récupération de la longueur du mot de passe
iLongueur = Len(sPwd)
 
'Vérification que le mot de passe est de longueur supérieur à 5 et inférieur à 8
 
If iLongueur < 5 Or iLongueur > 8 Then
 
    MsgBox "Le mot de passe doit avoir 5 caractères au minimum et 8 caractères au maximum", vbInformation, "Longueur mot de passe"
    txtMot.SetFocus
    Exit Sub
End If
 
'Récupération du numéro de la dernière ligne occupée
iVarTest = Sheets("mot").Range("A65000").End(xlUp).Row
 
'*****************************************************
'*****************************************************
Application.ScreenUpdating = False
 
'Initialisation de la variable de la boucle
i = 2
 
'Début de la boucle
Do
 
    'Vérification du nom d'utilisateur et du mot de passe
    If (sLogin = Sheets("mot").Cells(i, 1).Value) And (sPwd = Sheets("mot").Cells(i, 2).Value) Then
 
            'Vérification du profil de l'utilisateur
            If Sheets("mot").Cells(i, 3).Value = 0 Then
 
                Call ReactivationFeuille0
 
                'sortie de la boucle
                Exit Do
 
            '****************************************************
            ElseIf Sheets("mot").Cells(i, 3).Value = 1 Then
                Call ReactivationFeuille1
 
 
                'sortie de la boucle
                Exit Do
            '*****************************************************
 
            ElseIf Sheets("mot").Cells(i, 3).Value = 2 Then
                Call ReactivationFeuille2
 
 
                'sortie de la boucle
                Exit Do
            '*****************************************************
            Else
 
                MsgBox "Groupe inconnu", vbInformation, "Gestion des profils"
 
                Exit Do
 
            End If
   '***********************
   'Si code actif, une seule entrée dans la boucle
   '***********************
   'Alternative à problème
   'Else
   'MsgBox "Nom ou mot de passe incorrects", vbInformation, "Login"
   'Exit Do
    '***********************
    '***********************
 
   End If
 
'Icrémentation de la boucle
i = i + 1
 
'Sortie définitive de la boucle si la condition est remplie:
'(la valeur de i est supérieure au numéro de la dernière ligne occupée)
Loop While i <= iVarTest
 
 
End Sub

Why does the first "Else" of "If" does not work in this case.

I am missing something? I waiting for your help.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try "Select Case" instead of If/ElseIf:
Code:
Select Case Sheets("mot").Cells(i, 3).Value
    Case Is = 0
        Call ReactivationFeuille0
        'sortie de la boucle
            Exit Do
    
    Case Is = 1
        Call ReactivationFeuille1
        'sortie de la boucle
            Exit Do
    
    Case Is = 2
        Call ReactivationFeuille2
        'sortie de la boucle
            Exit Do
    
    Case Else
        MsgBox "Groupe inconnu", vbInformation, "Gestion des profils"
        Exit Do
End Select
 
Upvote 0
Try "Select Case" instead of If/ElseIf:
Code:
Select Case Sheets("mot").Cells(i, 3).Value
    Case Is = 0
        Call ReactivationFeuille0
        'sortie de la boucle
            Exit Do
    
    Case Is = 1
        Call ReactivationFeuille1
        'sortie de la boucle
            Exit Do
    
    Case Is = 2
        Call ReactivationFeuille2
        'sortie de la boucle
            Exit Do
    
    Case Else
        MsgBox "Groupe inconnu", vbInformation, "Gestion des profils"
        Exit Do
End Select

Thanks Misca for your quick reply.

This part of the code does not have a problem.
The problem is why this "Else" part does not work:
Code:
 'Else
   'MsgBox "Nom ou mot de passe incorrects", vbInformation, "Login"
   'Exit Do

I have to put it as a comment because if I leave it, the login/pwd from the third line to the end are not tested.
By letting it as a comment, the code works fine but if a user try to get in make the login and the password not matching, he/she won't get in, but nothing happens. Instead, i would have liked that whenever there is a mismatch between the login and the password, the message should tell the person that there is a problem ("Nom ou mot de passe incorrects).
I don't know whether i explain well.
 
Upvote 0
I have got the solution.
I used a boolean. It takes a default value. It changes the status within the Do/Loop structure if login and password are correct. If not it maintains the same value.

After the Loop part, i test the value and send a message if the default value remains the same.
That was the clue.
Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top