Redondance de question macro autre VBA code

Bonjour

dans ma macro je veux selectionner des lots et choisir leur numero. Leur selection se fait via un menu deroulant qui va chercher l'information dans sheet2, et le numero de lot on l'inscris dans la case correspondante dans sheet1. Une fois les information du lot A (A1,A2,A3) est entrée et le n umero est entré dans E4, j'ai le choix de continuer au lot B (B1,B2,B3) ou quitter la sub, ou encore aller au Lot C (C1,C2,C3). voici le code et le fichier test est attaché. Le problème est que après avoir dit non ou remplis le lot B et lot c la question revient toujours: voulez vous slectionner un deuxieme lot.

8extra-test.xlsm (28.18 Ko)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim AnswerYes As Integer

'========================Lot 1===============================
'============================================================
'Exit Sub

    If Worksheets("Sheet2").Cells(4, 16).Value = 1 Then

  Application.EnableEvents = False

         MsgBox ("Selectionner le Lot 1")

         Range("e3").Select

      Else

    If Worksheets("Sheet1").Cells(4, 5).Value = 0 Then

  Application.EnableEvents = False

          MsgBox ("Enter le numero du Lot 1")

          Range("e4").Select

     Else
'========================Lot 2===============================
'============================================================

   AnswerYes = MsgBox("Voulez-vous Selectionner un deuxieme Lot?", vbQuestion + vbYesNo, "User Repsonse")

If AnswerYes = vbYes Then

 If Worksheets("Sheet2").Cells(14, 16).Value = 1 Then

 Application.EnableEvents = False

        MsgBox ("Selectionner le Lot 2")
        Range("f3").Select
Else

If Worksheets("Sheet1").Cells(4, 6).Value = 0 Then

 Application.EnableEvents = False

        MsgBox ("Enter le numero du Lot 2")
        Range("f4").Select
 '========================Lot 3===============================
 '============================================================
 Else

   AnswerYes = MsgBox("Voulez-vous Selectionner un Troisieme Lot?", vbQuestion + vbYesNo, "User Repsonse")

If AnswerYes = vbYes Then

 If Worksheets("Sheet2").Cells(24, 16).Value = 1 Then

 Application.EnableEvents = False

        MsgBox ("Selectionner le Lot 3")
        Range("g3").Select
Else

If Worksheets("Sheet1").Cells(4, 7).Value = 0 Then

 Application.EnableEvents = False

        MsgBox ("Enter le numero du Lot 3")
        Range("g4").Select
 '============================================================
 '============================================================

              End If

          End If

'Else: Exit Sub

        End If

     End If

   End If
'Else: Exit Sub
  End If

 End If

End If
Application.EnableEvents = True

End Sub
7extra-test.xlsm (28.16 Ko)

Bonsoir

"Private Sub Worksheet_SelectionChange(ByVal Target As Range)"

ça veut dire qu'à chaque fois que tu changes de cellule tu relance la macro donc tu n'en sort jamais...

Je ne comprends pas l'utilité de passer par un évènement, surtout SelectionChange si encore c'était un simple Change peut être...

Cordialement

Avec un seul lot ca fonctionne tres bien

Rebonjour. Je partage avec vous ma solution presque parfaite:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim AnswerYes As Integer

'========================Lot 1===============================

'============================================================

    If Worksheets("Sheet2").Cells(4, 16).Value = 1 Then

  Application.EnableEvents = False

         MsgBox ("Selectionner le Lot 1")
         Range("e3").Select

      Else

    If Worksheets("Sheet1").Cells(4, 5).Value = 0 Then

  Application.EnableEvents = False

          MsgBox ("Enter le numero du Lot 1")
          Range("e4").Select

     Else
'========================Lot 2===============================
'============================================================

   AnswerYes = MsgBox("Voulez-vous continuer pour selectionner un autre Lot?", vbQuestion + vbYesNo, "User Repsonse")

Application.EnableEvents = False

If AnswerYes = vbNo Then

Exit Sub

Else

Application.EnableEvents = False

 If Worksheets("Sheet2").Cells(14, 16).Value = 1 Then

 Application.EnableEvents = False

        MsgBox ("Selectionner le Lot 2")
        Range("f3").Select
Else
'============================================================

'============================================================

If Worksheets("Sheet1").Cells(4, 6).Value = 0 Then

 Application.EnableEvents = False

        MsgBox ("Enter le numero du Lot 2")
        Range("f4").Select

    Else
 '========================Lot 3===============================

 '============================================================

Application.EnableEvents = False

 If Worksheets("Sheet2").Cells(24, 16).Value = 1 Then

 Application.EnableEvents = False

        MsgBox ("Selectionner le Lot 3")
        Range("g3").Select
'============================================================

'============================================================
Else

If Worksheets("Sheet1").Cells(4, 7).Value = 0 Then

 Application.EnableEvents = False

        MsgBox ("Enter le numero du Lot 3")
        Range("g4").Select

 '============================================================
 '============================================================
 '============================================================

'============================================================
Exit Sub
              End If

          End If

        End If

     End If

   End If

  End If

 End If

Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim AnswerYes As Integer

'========================Lot 1===============================
If Worksheets("Sheet2").Cells(4, 16).Value > 1 And Worksheets("Sheet2").Cells(14, 16).Value > 1 And Worksheets("Sheet2").Cells(24, 16).Value > 1 And Worksheets("Sheet1").Cells(4, 5).Value > 0 And Worksheets("Sheet1").Cells(4, 6).Value > 0 And Worksheets("Sheet1").Cells(4, 7).Value > 0 Then
'============================================================
Exit Sub
Else

    If Worksheets("Sheet2").Cells(4, 16).Value = 1 Then

  Application.EnableEvents = False

         MsgBox ("Selectionner le Lot 1")
         Range("e3").Select

      Else

    If Worksheets("Sheet1").Cells(4, 5).Value = 0 Then

  Application.EnableEvents = False

          MsgBox ("Enter le numero du Lot 1")
          Range("e4").Select

     Else
'========================Lot 2===============================
'============================================================

   AnswerYes = MsgBox("Voulez-vous continuer pour selectionner un autre Lot?", vbQuestion + vbYesNo, "User Repsonse")

Application.EnableEvents = False

If AnswerYes = vbNo Then

Exit Sub

Else

Application.EnableEvents = False

 If Worksheets("Sheet2").Cells(14, 16).Value = 1 Then

 Application.EnableEvents = False

        MsgBox ("Selectionner le Lot 2")
        Range("f3").Select
Else
'============================================================

'============================================================

If Worksheets("Sheet1").Cells(4, 6).Value = 0 Then

 Application.EnableEvents = False

        MsgBox ("Enter le numero du Lot 2")
        Range("f4").Select

    Else
 '========================Lot 3===============================

 '============================================================

Application.EnableEvents = False

 If Worksheets("Sheet2").Cells(24, 16).Value = 1 Then

 Application.EnableEvents = False

        MsgBox ("Selectionner le Lot 3")
        Range("g3").Select
'============================================================

'============================================================
Else

If Worksheets("Sheet1").Cells(4, 7).Value = 0 Then

 Application.EnableEvents = False

        MsgBox ("Enter le numero du Lot 3")
        Range("g4").Select

 '============================================================
 '============================================================
 '============================================================

'============================================================
'Exit Sub
              End If

          End If

        End If

     End If

   End If

  End If

 End If
End If
Application.EnableEvents = True

End Sub
3extra-test.xlsm (35.14 Ko)
Rechercher des sujets similaires à "redondance question macro vba code"