IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Amélioration code vba


Sujet :

Macros et VBA Excel

  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2025
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mars 2025
    Messages : 1
    Par défaut Amélioration code vba
    Bonjour enfaite ce que j'aimerais réaliser est de ne pas pouvoir valider avec le bouton ok tant que toutes les sélections ne sont pas faites dans les listes déroulantes combobox 1234 bien sur a la derniere valeur cependant ca ne grise pas les combobox qui n'ont pas de valeur
    'Initialisation du formulaire de DECLARATION DE L'ARRÊT
    Private Sub UserForm1_Initialize()
    UpdateComboBox1
    End Sub
    'Gestion de la checkbox 7 (Oui)Arret planifié (Non TRS)
    Private Sub CheckBox7_Click()
    If CheckBox7.value = True Then

    CheckBox6.value = False
    CheckBox1.value = False
    CheckBox2.value = False
    CheckBox1.Enabled = False
    CheckBox2.Enabled = False
    Label7.Enabled = False
    Label6.Enabled = False
    UpdateCheckBox345State

    Else
    CheckBox6.value = True
    CheckBox6_Click

    End If
    UpdateComboBox1

    End Sub
    'Gestion de la checkbox 6 (Non)Arret planifié (Non TRS)
    Private Sub CheckBox6_Click()
    If CheckBox6.value = True Then
    CheckBox7.value = False
    Label6.Enabled = True
    CheckBox2.Enabled = True
    CheckBox1.Enabled = True
    Else
    CheckBox7.value = True
    CheckBox7_Click
    End If

    UpdateComboBox1
    End Sub
    'Gestion de la checkbox 1 (Non) Micro Arret
    Private Sub CheckBox1_Click()
    If CheckBox1.value = True Then
    CheckBox2.value = False
    Label7.Enabled = False
    UpdateCheckBox345State

    Else
    CheckBox2.value = True
    CheckBox2_Click
    End If
    End Sub
    'Gestion de la checkbox 2 (Oui) Micro Arret
    Private Sub CheckBox2_Click()
    If CheckBox2.value = True Then
    CheckBox1.value = False
    Label7.Enabled = True
    CheckBox3.value = True
    CheckBox3.Enabled = True
    CheckBox4.Enabled = shouldBeEnabled
    CheckBox5.Enabled = shouldBeEnabled
    UserForm2.Label1.Enabled = False
    UserForm2.TextBox1.Enabled = False
    UserForm2.TextBox1.Text = "Matin"

    Else
    UserForm2.Label1.Enabled = True
    UserForm2.TextBox1.Enabled = True
    UserForm2.TextBox1.Text = ""
    CheckBox1.value = True
    CheckBox1_Click
    UpdateCheckBox345State
    End If
    End Sub
    ' Gestion de l'activation visuel de la ligne Micro Arret
    Private Sub UpdateCheckBox345State()
    Dim shouldBeEnabled As Boolean
    shouldBeEnabled = Not CheckBox1.value

    CheckBox3.Enabled = shouldBeEnabled
    CheckBox4.Enabled = shouldBeEnabled
    CheckBox5.Enabled = shouldBeEnabled
    Label7.Enabled = shouldBeEnabled
    If Not shouldBeEnabled Then
    CheckBox3.value = False
    CheckBox4.value = False
    CheckBox5.value = False
    End If
    End Sub
    ' Gestion CheckBox 3,4,8 (Matin, Midi, Soir) mutuellement exclusives
    Private Sub CheckBox3_Click() ' Gestion CheckBox 3(Matin)
    If CheckBox3.value = True Then
    CheckBox4.value = False
    CheckBox5.value = False
    End If
    End Sub

    Private Sub CheckBox4_Click() ' Gestion CheckBox 4(Soir)
    If CheckBox4.value = True Then
    CheckBox3.value = False
    CheckBox5.value = False
    End If
    End Sub

    Private Sub CheckBox5_Click() ' Gestion CheckBox 5(Nuit)
    If CheckBox5.value = True Then
    CheckBox3.value = False
    CheckBox4.value = False
    End If
    End Sub
    ' Permet de renvoyer la colonne de la ligne active en fonction de la machine
    Function CorrespondanceColonneRC() As Integer
    Dim wsRC As Worksheet
    Set wsRC = ThisWorkbook.Sheets("CorrespondanceRC")

    Dim LigneName As String
    LigneName = UCase(ActiveSheet.Range("H2").value) ' Récupère le nom de la ligne

    Select Case True
    Case (Len(LigneName) = 4) And (Left(LigneName, 2) = "RG") And IsNumeric(Right(LigneName, 2)) 'Si RG alors 'RGXX'
    LigneName = "RGXX"
    Case (Len(LigneName) = 4) And (Left(LigneName, 2) = "AT") And IsNumeric(Right(LigneName, 2)) 'Si AT alors 'RGXX'
    LigneName = "ATXX"
    End Select

    Dim i As Long
    Dim lastCol As Long
    lastCol = wsRC.Cells(3, wsRC.Columns.Count).End(xlToLeft).Column 'compte le nombre de colonne à parcourir

    ' Boucle à partir de la colonne F (colonne 6) jusqu'à la dernière colonne
    For i = 6 To lastCol
    If UCase(Left(wsRC.Cells(3, i).value, 7)) = "ACTIVER" Then 'Recherche des colonnes "Activer"
    Dim colMachine As String
    colMachine = Replace(Mid(UCase(wsRC.Cells(3, i).value), 8), vbLf, "") ' Récupere la ligne
    If Left(colMachine, 1) = " " Then
    colMachine = Mid(colMachine, 2) ' Supprimer le premier caractère (l'espace)
    End If

    If colMachine = LigneName Then ' On récupere la colonne
    CorrespondanceColonneRC = i
    Exit Function
    End If
    End If
    Next i

    CorrespondanceColonneRC = -1
    End Function
    ' Mise a jour de la combobox 1 en fonction de la machine
    Sub UpdateComboBox1()
    Dim ws As Worksheet
    Dim wsActive As Worksheet
    Dim nomMachine As String
    Dim colMachine As Integer

    Set ws = ThisWorkbook.Sheets("CorrespondanceRC")
    Set wsActive = ActiveSheet

    colMachine = CorrespondanceColonneRC()

    If colMachine < 0 Then Exit Sub ' Sortir si la colonne n'est pas reconnue

    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    Dim dict1 As Object
    Set dict1 = CreateObject("Scripting.Dictionary")

    Dim typeArret As String
    If CheckBox7.value Then
    typeArret = "SCHED_DOWN"
    Else
    typeArret = "UNSCD_DOWN"
    End If

    ComboBox1.Clear
    Dim i As Long
    For i = 4 To lastRow
    If ws.Cells(i, 1).value = typeArret Then
    If Not IsEmpty(ws.Cells(i, colMachine).value) Then
    dict1(ws.Cells(i, 2).value) = 1
    End If
    End If
    Next i

    Dim key As Variant
    For Each key In dict1.Keys
    ComboBox1.AddItem key
    Next key
    End Sub
    ' Gestion du changement de combobox 1
    Private Sub ComboBox1_Change()
    UpdateComboBox2
    UpdateComboBoxStates
    Commandbutton1.Enabled = True
    End Sub
    ' Mise a jour de la combobox 2 en fonction du choix dans combobox 1
    Sub UpdateComboBox2()
    Dim ws As Worksheet
    Dim wsActive As Worksheet
    Dim nomMachine As String
    Dim colMachine As Integer

    Set ws = ThisWorkbook.Sheets("CorrespondanceRC")
    Set wsActive = ActiveSheet

    colMachine = CorrespondanceColonneRC()

    If colMachine < 0 Then Exit Sub

    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    Dim dict2 As Object
    Set dict2 = CreateObject("Scripting.Dictionary")

    Dim typeArret As String
    If CheckBox7.value Then
    typeArret = "SCHED_DOWN"
    Else
    typeArret = "UNSCD_DOWN"
    End If

    ComboBox2.Clear
    Dim i As Long
    For i = 4 To lastRow
    If ws.Cells(i, 1).value = typeArret And ws.Cells(i, 2).value = ComboBox1.value Then
    If Not IsEmpty(ws.Cells(i, colMachine).value) Then
    dict2(ws.Cells(i, 3).value) = 1
    End If
    End If
    Next i

    ' Gestion de l'état de la ComboBox2 selon le contenu du dictionnaire
    If dict2.Count = 0 Then
    ComboBox2.Enabled = False
    ComboBox2.value = ""
    ' Désactiver et vider aussi les ComboBox suivantes
    ComboBox3.Enabled = False
    ComboBox3.value = ""
    ComboBox4.Enabled = False
    ComboBox4.value = ""
    Else
    ComboBox2.Enabled = True
    Dim key As Variant
    For Each key In dict2.Keys
    ComboBox2.AddItem key
    Next key
    End If
    End Sub
    ' Gestion du changement de combobox 2
    Private Sub ComboBox2_Change()
    UpdateComboBox3
    UpdateComboBoxStates
    Commandbutton1.Enabled = True
    End Sub
    ' Mise a jour de la combobox 3 en fonction du choix dans combobox 2
    Sub UpdateComboBox3()
    Dim ws As Worksheet
    Dim wsActive As Worksheet
    Dim nomMachine As String
    Dim colMachine As Integer

    Set ws = ThisWorkbook.Sheets("CorrespondanceRC")
    Set wsActive = ActiveSheet

    colMachine = CorrespondanceColonneRC()

    If colMachine < 0 Then Exit Sub

    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    Dim dict3 As Object
    Set dict3 = CreateObject("Scripting.Dictionary")

    Dim typeArret As String
    If CheckBox7.value Then
    typeArret = "SCHED_DOWN"
    Else
    typeArret = "UNSCD_DOWN"
    End If

    ComboBox3.Clear
    Dim i As Long
    For i = 4 To lastRow
    If ws.Cells(i, 1).value = typeArret And ws.Cells(i, 3).value = ComboBox2.value Then
    If Not IsEmpty(ws.Cells(i, colMachine).value) Then
    dict3(ws.Cells(i, 4).value) = 1
    End If
    End If
    Next i

    ' Gestion de l'état de la ComboBox3 selon le contenu du dictionnaire
    If dict3.Count = 0 Then
    ComboBox3.Enabled = False
    ComboBox3.value = ""
    ' Désactiver et vider aussi la ComboBox suivante
    ComboBox4.Enabled = False
    ComboBox4.value = ""
    Else
    ComboBox3.Enabled = True
    Dim key As Variant
    For Each key In dict3.Keys
    ComboBox3.AddItem key
    Next key
    End If
    End Sub
    ' Gestion du changement de combobox 3
    Private Sub ComboBox3_Change()
    UpdateComboBox4
    UpdateComboBoxStates
    Commandbutton1.Enabled = True
    End Sub
    ' Mise a jour de la combobox 4 en fonction du choix dans combobox 3
    Sub UpdateComboBox4()
    Dim ws As Worksheet
    Dim wsActive As Worksheet
    Dim nomMachine As String
    Dim colMachine As Integer

    Set ws = ThisWorkbook.Sheets("CorrespondanceRC")
    Set wsActive = ActiveSheet
    ws.Unprotect Password:="loiret"

    colMachine = CorrespondanceColonneRC()

    If colMachine < 0 Then Exit Sub

    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    Dim dict4 As Object
    Set dict4 = CreateObject("Scripting.Dictionary")

    Dim typeArret As String
    If CheckBox7.value Then
    typeArret = "SCHED_DOWN"
    Else
    typeArret = "UNSCD_DOWN"
    End If

    ComboBox4.Clear
    Dim i As Long
    For i = 4 To lastRow
    If ws.Cells(i, 1).value = typeArret And ws.Cells(i, 4).value = ComboBox3.value Then
    If Not IsEmpty(ws.Cells(i, colMachine).value) Then
    dict4(ws.Cells(i, 5).value) = 1
    End If
    End If
    Next i

    ' Gestion de l'état de la ComboBox4 selon le contenu du dictionnaire
    If dict4.Count = 0 Then
    ComboBox4.Enabled = False
    ComboBox4.value = ""
    Else
    ComboBox4.Enabled = True
    Dim key As Variant
    For Each key In dict4.Keys
    ComboBox4.AddItem key
    Next key
    End If
    End Sub
    Private Sub UpdateComboBoxStates()
    ' Désactiver toutes les ComboBox par défaut
    ComboBox2.Enabled = False
    ComboBox3.Enabled = False
    ComboBox4.Enabled = False

    ' Activer ComboBox2 si ComboBox1 a une valeur sélectionnée et des éléments
    If ComboBox1.ListCount > 0 And ComboBox1.value <> "" Then
    ComboBox2.Enabled = True
    End If

    ' Activer ComboBox3 si ComboBox2 a une valeur sélectionnée et des éléments
    If ComboBox2.ListCount > 0 And ComboBox2.value <> "" Then
    ComboBox3.Enabled = True
    End If

    ' Activer ComboBox4 si ComboBox3 a une valeur sélectionnée et des éléments
    If ComboBox3.ListCount > 0 And ComboBox3.value <> "" Then
    ComboBox4.Enabled = True
    End If
    End Sub
    ' Gestion du changement de combobox 4
    Private Sub ComboBox4_Change()
    UpdateComboBoxStates
    End Sub
    'Gestion du bouton Ok
    Private Sub CommandButton1_Click()
    ' Vérifier qu'une valeur est sélectionnée
    Dim valeurAEnregistrer As String
    If ComboBox4.value <> "" Then
    valeurAEnregistrer = ComboBox4.value
    ElseIf ComboBox3.value <> "" Then
    valeurAEnregistrer = ComboBox3.value
    ElseIf ComboBox2.value <> "" Then
    valeurAEnregistrer = ComboBox2.value
    ElseIf ComboBox1.value <> "" Then
    valeurAEnregistrer = ComboBox1.value
    Else
    MsgBox "Veuillez sélectionner au moins une valeur", vbExclamation
    Exit Sub
    End If

    ' Trouver la première ligne vide dans la colonne O à partir de O6
    Dim NextRow As Long
    NextRow = 6
    Do While Not IsEmpty(ThisWorkbook.ActiveSheet.Cells(NextRow, "O"))
    NextRow = NextRow + 1
    Loop

    ' Enregistrer la valeur
    ActiveSheet.Unprotect Password:="loiret"
    ThisWorkbook.ActiveSheet.Cells(NextRow, "O").value = valeurAEnregistrer

    ' Stocker les valeurs individuelles des combobox dans Z, AA, AB, AC
    ThisWorkbook.ActiveSheet.Cells(NextRow, "Z").value = ComboBox1.value
    ThisWorkbook.ActiveSheet.Cells(NextRow, "AA").value = ComboBox2.value
    ThisWorkbook.ActiveSheet.Cells(NextRow, "AB").value = ComboBox3.value
    ThisWorkbook.ActiveSheet.Cells(NextRow, "AC").value = ComboBox4.value

    ' Vérifier l'état de CheckBox7 et mettre la valeur appropriée dans la colonne X
    If CheckBox7.value = True Then
    ThisWorkbook.ActiveSheet.Cells(NextRow, "X").value = "SCHED_DOWN"
    ElseIf CheckBox6.value = True Then
    ThisWorkbook.ActiveSheet.Cells(NextRow, "X").value = "UNSCHED_DOWN"
    End If

    ' Vider les ComboBox après l'enregistrement
    ComboBox1.value = ""
    ComboBox2.value = ""
    ComboBox3.value = ""
    ComboBox4.value = ""

    ' Ouvrir UserForm2 et fermer UserForm1
    Unload Me
    UserForm2.Show
    End Sub
    'Gestion du bouton "ANNULER"
    Private Sub CommandButton2_Click()
    Unload Me
    End Sub


    Private Sub Label13_Click()

    End Sub

    Private Sub UserForm_Click()

    End Sub

  2. #2
    Membre Expert
    Inscrit en
    Décembre 2002
    Messages
    993
    Détails du profil
    Informations forums :
    Inscription : Décembre 2002
    Messages : 993
    Par défaut
    Salut, voici une suggestion (si j'ai bien compris).
    Ajoute cette fonction pour vérifier si toutes les ComboBox sont bien remplies :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Private Sub VerifierValidation()
        ' Désactiver le bouton OK par défaut
        CommandButton1.Enabled = False
     
        ' Activer uniquement si toutes les combobox ont une valeur sélectionnée
        If ComboBox1.value <> "" And ComboBox2.value <> "" And ComboBox3.value <> "" And ComboBox4.value <> "" Then
            CommandButton1.Enabled = True
        End If
    End Sub
    Ajoute l’appel à VerifierValidation dans chaque événement Change() des ComboBox, par exemple:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Private Sub ComboBox1_Change()
        UpdateComboBox2
        UpdateComboBoxStates
        VerifierValidation ' Vérifier si le bouton OK peut être activé
    End Sub
    Ajoute cette ligne dans l’initialisation du formulaire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Private Sub UserForm_Initialize()
        UpdateComboBox1
        CommandButton1.Enabled = False ' Désactiver le bouton OK au début
    End Sub

Discussions similaires

  1. afficher un graphique dans word à partir de mon code vba
    Par guysocode dans le forum VBA Word
    Réponses: 2
    Dernier message: 07/11/2005, 14h15
  2. Réponses: 2
    Dernier message: 27/10/2005, 15h51
  3. Réponses: 4
    Dernier message: 13/10/2005, 14h44
  4. Réponses: 3
    Dernier message: 06/09/2005, 10h27
  5. Comment creer une procédure stockée à partir d'un code VBA?
    Par Alcor020980 dans le forum Connexion aux bases de données
    Réponses: 4
    Dernier message: 24/05/2005, 19h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo