VBA

Résolu
Jenni - 21 mai 2023 à 16:05
 Jenni - 26 mai 2023 à 02:04

Bonjour,

À l'aide d'un tuto, j'ai copier une macro qui fonctionne bien, mon formulaire se complète bien, mais complet l'onglet 1 de mon classeur. Seulement, j'ai plusieurs feuilleS dans mon classeur Excel et j'essaie de compléter un tableau qui se nomme Liste_candidat dans la feuille qui se nomme Sheet1 (CANDIDATS). J'ai fait plusieurs essaie en changeant les variables dans mon code, mais il a toujours quelque chose qui ne fonctionne pas. Quelqu'un peu m'aider ?

Merci à l'avance!

Private Sub BtAjouter_Click()

Dim onglet As Worksheet
Dim derniere_ligne As Long
Dim No As Long

Set onglet = Worksheets(CANDIDATS - RH)

derniere_ligne = onglet.Cells(Rows.Count, 1).End(xlUp).Row

If dernere_ligne = 1 Then
    No = 1
Else
    No = onglet.Cells(derniere_ligne, 1) + 1
End If

With onglet

    .Cells(derniere_ligne + 1, 1) = No
    .Cells(derniere_ligne + 1, 2) = tbxNom
    .Cells(derniere_ligne + 1, 3) = tbxCourriel
    .Cells(derniere_ligne + 1, 4) = TbxTel
    .Cells(derniere_ligne + 1, 5) = TbxVille
    .Cells(derniere_ligne + 1, 6) = TbxSecteur
    .Cells(derniere_ligne + 1, 7) = BoxChoix1
    .Cells(derniere_ligne + 1, 8) = BoxChoix2
    .Cells(derniere_ligne + 1, 9) = BoxChoix3
    .Cells(derniere_ligne + 1, 10) = BoxEntretien
    .Cells(derniere_ligne + 1, 11) = TbxDate
    .Cells(derniere_ligne + 1, 12) = TbxDoc
    .Cells(derniere_ligne + 1, 13) = BoxStatut
    .Cells(derniere_ligne + 1, 14) = TbxQualification
    .Cells(derniere_ligne + 1, 15) = TbxQuestionnaire
    .Cells(derniere_ligne + 1, 16) = TbxRemarque
    .Cells(derniere_ligne + 1, 17) = BoxDispo
End With

End Sub

10 réponses

jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
21 mai 2023 à 19:15

Bonjour,

Pour commencer

Set onglet = Worksheets("CANDIDATS - RH")

1

Bien sûr !! Je ne voyais plus claire on dirait bien. Merci beaucoup!

Et si je veux que les infos s'inscrivent dans mon tableau structuré qui se nomme Liste_candidat, vous savez aussi comment ? ;)

0
thev Messages postés 1853 Date d'inscription lundi 7 avril 2008 Statut Membre Dernière intervention 27 avril 2024 681
23 mai 2023 à 09:55

Pour terminer, au cas où, code pour la suppression

Private Sub BtSupprimer_Click()
    Dim i As Long
    
    'recherche indice ligne tableau à partir de la valeur de la Listbox (no)
    With [Liste_candidats].ListObject
        i = 0
        On Error Resume Next
        i = Application.Match(ListBox1.Value, .ListColumns(1).DataBodyRange, 0)
        On Error GoTo 0
        If i = 0 Then MsgBox "aucune ligne à supprimer": Exit Sub
        
        'suppression
        .ListRows(i).Delete
        
        'chargement Listbox
        ListBox1.Clear
        If .ListRows.Count > 0 Then ListBox1.List = .DataBodyRange.Value
    End With
    
End Sub

1
thev Messages postés 1853 Date d'inscription lundi 7 avril 2008 Statut Membre Dernière intervention 27 avril 2024 681
Modifié le 21 mai 2023 à 22:53

Bonsoir,

ci-dessous code pour votre tableau structuré Liste_candidat :

Private Sub BtAjouter_Click()

    Dim ligne As ListRow
    Dim i As Long, no As Long
    
    With [Liste_candidat].ListObject
        Set ligne = .ListRows.Add: i = ligne.Index   'ajout ligne à la fin du tableau
        
        no = Application.Max(.ListColumns(1).DataBodyRange.Value) + 1 'calcul numéro
        .ListColumns(1).DataBodyRange(i) = no
        .ListColumns(2).DataBodyRange(i) = tbxNom
        .ListColumns(3).DataBodyRange(i) = tbxCourriel
        .ListColumns(4).DataBodyRange(i) = TbxTel
        .ListColumns(5).DataBodyRange(i) = TbxVille
        .ListColumns(6).DataBodyRange(i) = TbxSecteur
        .ListColumns(7).DataBodyRange(i) = BoxChoix1
        .ListColumns(8).DataBodyRange(i) = BoxChoix2
        .ListColumns(9).DataBodyRange(i) = BoxChoix3
        .ListColumns(10).DataBodyRange(i) = BoxEntretien
        .ListColumns(11).DataBodyRange(i) = TbxDate
        .ListColumns(12).DataBodyRange(i) = TbxDoc
        .ListColumns(13).DataBodyRange(i) = BoxStatut
        .ListColumns(14).DataBodyRange(i) = TbxQualification
        .ListColumns(15).DataBodyRange(i) = TbxQuestionnaire
        .ListColumns(16).DataBodyRange(i) = TbxRemarque
        .ListColumns(17).DataBodyRange(i) = BoxDispo
    
    End With

End Sub
0

Oh wow merci beaucoup!

Par contre un message apparait "La méthode 'Add' de l'objet 'ListRows' a échoué.

J'ai fait des recherches pour essayer de comprendre, mais je n'y arrive pas. 

0
thev Messages postés 1853 Date d'inscription lundi 7 avril 2008 Statut Membre Dernière intervention 27 avril 2024 681
22 mai 2023 à 00:19

Par contre un message apparait "La méthode 'Add' de l'objet 'ListRows' a échoué.

Si vous avez ce message, c'est que votre tableau structuré ne s'appelle pas exactement  Liste_candidat. Peut être avez-vous un blanc après le t ? 

Vérifiez le nom de votre tableau en le sélectionnant complètement.


0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question

Effectivement, il manquait un s, mais maintenant l'application se ferme automatique après voir afficher le message d'erreur.

0

Finalement, après plusieurs essaie, ça semble fonctionné d'autre fois non, tout ferme et je ne comprend pas pourquoi.

0
thev Messages postés 1853 Date d'inscription lundi 7 avril 2008 Statut Membre Dernière intervention 27 avril 2024 681
Modifié le 22 mai 2023 à 16:21

Vérifiez que votre tableau "Liste_candidats" n'est pas utilisé  dans la propriété RowSource  d'une Combobox ou d'une ListBox (cette propriété ne peut être utilisée que pour un tableau non modifié à l'exécution)

Sinon joignez un fichier anonymisé via https://www.cjoint.com/


0

Oui c'est bien ça, j'ai insérer une liste afin de pouvoir visualiser les candidats déjà ajoutés pour pouvoir les modifier par la suite, donc je comprends que je ne peux pas faire ça ainsi.

Je vais continuer à regarder comment faire pour ces prochaines étapes. Au moins l'ajout de candidats fonctionne maintenant, je vous remercie de votre précieux temps !!

0
thev Messages postés 1853 Date d'inscription lundi 7 avril 2008 Statut Membre Dernière intervention 27 avril 2024 681
Modifié le 22 mai 2023 à 18:15

Si vous voulez insérer par exemple une ListBox pour visualiser les candidats, vous devez utiliser alors la propriété List à l'exécution.

Ci-dessous code affichant dans une ListBox les numéros et noms des candidats

Option Explicit

Private Sub UserForm_Initialize()

    ListBox1.ColumnCount = 2
    With [Liste_candidats].ListObject
        If .ListRows.Count > 0 Then ListBox1.List = .DataBodyRange.Value
    End With

End Sub


Private Sub BtAjouter_Click()

    Dim ligne As ListRow
    Dim i As Long, no As Long
    Dim ctrl As Control
    
    With [Liste_candidats].ListObject
        Set ligne = .ListRows.Add: i = ligne.Index   'ajout ligne à la fin du tableau
        
        no = Application.Max(.ListColumns(1).DataBodyRange.Value) + 1 'calcul numéro
        .ListColumns(1).DataBodyRange(i) = no
        .ListColumns(2).DataBodyRange(i) = tbxNom
        .ListColumns(3).DataBodyRange(i) = tbxCourriel
        .ListColumns(4).DataBodyRange(i) = TbxTel
        .ListColumns(5).DataBodyRange(i) = TbxVille
        .ListColumns(6).DataBodyRange(i) = TbxSecteur
        .ListColumns(7).DataBodyRange(i) = BoxChoix1
        .ListColumns(8).DataBodyRange(i) = BoxChoix2
        .ListColumns(9).DataBodyRange(i) = BoxChoix3
        .ListColumns(10).DataBodyRange(i) = BoxEntretien
        .ListColumns(11).DataBodyRange(i) = TbxDate
        .ListColumns(12).DataBodyRange(i) = TbxDoc
        .ListColumns(13).DataBodyRange(i) = BoxStatut
        .ListColumns(14).DataBodyRange(i) = TbxQualification
        .ListColumns(15).DataBodyRange(i) = TbxQuestionnaire
        .ListColumns(16).DataBodyRange(i) = TbxRemarque
        .ListColumns(17).DataBodyRange(i) = BoxDispo
    
        ListBox1.List = .DataBodyRange.Value

    End With
    
    '// réinitialisation des textbox
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is MSForms.TextBox Then ctrl = Empty
    Next ctrl

End Sub

0

Merveilleux !! Ça fonctionne parfaitement bien. Un grand merci à vous. :)

0

Et est-ce bien complexe si je veux afficher les informations d'un candidat sélectionné dans mon formulaire pour pourvoir les modifier par la suite ?

0
thev Messages postés 1853 Date d'inscription lundi 7 avril 2008 Statut Membre Dernière intervention 27 avril 2024 681
22 mai 2023 à 23:42

Non. ci-dessous exemple de code :

Option Explicit

Private Sub UserForm_Initialize()

    ListBox1.ColumnCount = 2
    With [Liste_candidats].ListObject
        If .ListRows.Count > 0 Then ListBox1.List = .DataBodyRange.Value
    End With

End Sub

Private Sub ListBox1_Click()
    Dim i As Long
    
    With [Liste_candidats].ListObject
        
        'recherche indice ligne tableau à partir de la valeur de la Listbox (no)
        i = Application.Match(ListBox1.Value, .ListColumns(1).DataBodyRange, 0)
        
        'affichage ligne sélectionnée
        tbxNom = .ListColumns(2).DataBodyRange(i)
        tbxCourriel = .ListColumns(3).DataBodyRange(i)
        TbxTel = .ListColumns(4).DataBodyRange(i)
        TbxVille = .ListColumns(5).DataBodyRange(i)
        TbxSecteur = .ListColumns(6).DataBodyRange(i)
        BoxChoix1 = .ListColumns(7).DataBodyRange(i)
        BoxChoix2 = .ListColumns(8).DataBodyRange(i)
        BoxChoix3 = .ListColumns(9).DataBodyRange(i)
        BoxEntretien = .ListColumns(10).DataBodyRange(i)
        TbxDate = .ListColumns(11).DataBodyRange(i)
        TbxDoc = .ListColumns(12).DataBodyRange(i)
        BoxStatut = .ListColumns(13).DataBodyRange(i)
        TbxQualification = .ListColumns(14).DataBodyRange(i)
        TbxQuestionnaire = .ListColumns(15).DataBodyRange(i)
        TbxRemarque = .ListColumns(16).DataBodyRange(i)
        BoxDispo = .ListColumns(17).DataBodyRange(i)

    End With

End Sub

Private Sub BtAjouter_Click()
    Dim ligne As ListRow
    Dim i As Long, no As Long
    
    
    'insertion ligne
    With [Liste_candidats].ListObject
        Set ligne = .ListRows.Add: i = ligne.Index   'ajout ligne à la fin du tableau
        no = Application.Max(.ListColumns(1).DataBodyRange.Value) + 1 'calcul numéro
        .ListColumns(1).DataBodyRange(i) = no
    End With
    
    'mise à jour du tableau
    maj_tableau i
    
End Sub

Private Sub BtModifier_Click()
    Dim i As Long
    
    'recherche indice ligne tableau à partir de la valeur de la Listbox (no)
    With [Liste_candidats].ListObject
        i = 0
        On Error Resume Next
        i = Application.Match(ListBox1.Value, .ListColumns(1).DataBodyRange, 0)
        On Error GoTo 0
        If i = 0 Then MsgBox "aucune ligne à modifier": Exit Sub
    End With

    'mise à jour du tableau
    maj_tableau i
    
End Sub

Private Sub maj_tableau(i As Long)
    Dim ctrl As Control
    
    With [Liste_candidats].ListObject
        
        'remplissage du tableau
        .ListColumns(2).DataBodyRange(i) = tbxNom
        .ListColumns(3).DataBodyRange(i) = tbxCourriel
        .ListColumns(4).DataBodyRange(i) = TbxTel
        .ListColumns(5).DataBodyRange(i) = TbxVille
        .ListColumns(6).DataBodyRange(i) = TbxSecteur
        .ListColumns(7).DataBodyRange(i) = BoxChoix1
        .ListColumns(8).DataBodyRange(i) = BoxChoix2
        .ListColumns(9).DataBodyRange(i) = BoxChoix3
        .ListColumns(10).DataBodyRange(i) = BoxEntretien
        .ListColumns(11).DataBodyRange(i) = TbxDate
        .ListColumns(12).DataBodyRange(i) = TbxDoc
        .ListColumns(13).DataBodyRange(i) = BoxStatut
        .ListColumns(14).DataBodyRange(i) = TbxQualification
        .ListColumns(15).DataBodyRange(i) = TbxQuestionnaire
        .ListColumns(16).DataBodyRange(i) = TbxRemarque
        .ListColumns(17).DataBodyRange(i) = BoxDispo
        
        'chargement Listbox
        ListBox1.List = .DataBodyRange.Value

    End With
    
    '// réinitialisation des textbox
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is MSForms.TextBox Then ctrl = Empty
    Next ctrl

End Sub

0

Wow je vous remercie énormément, ça fonctionne encore une fois à merveille !!! Je suis sur ce projet depuis plusieurs semaines à essayer seule de trouver, mais votre aide m'a permis de pouvoir lancer ce projet enfin ! Merci encore et bonne soirée!

0

J'aurais une dernière question, j'essaie de récupérer la donnée entrée dans la textbox tbxNom pour la coller dans mon code qui fait l'action d'envoyer un courriel, mais il me manque quelque chose. 

Sub EnvoyerCourrielNouveauCandidat()
            
    Dim LeMail As Variant
    
      
    Set LeMail = CreateObject("Outlook.Application")

    With LeMail.CreateItem(olMailItem)
        .Subject = "Séquence d'embauche - Nouveau candidat ajouté"
        .To = 
        .htmlbody = "Bonjour" & ",<br><br>" & _
                    "Un candidat a été ajouté :" & "<br><br>" & _
                    ".FrmCandidats.tbxNom.Value" & "<br><br>" & _
                    "Belle journée!"
                    
        .send
    End With
    Range("E6").Select

End Sub

Merci encore.

0
thev Messages postés 1853 Date d'inscription lundi 7 avril 2008 Statut Membre Dernière intervention 27 avril 2024 681 > Jenni
24 mai 2023 à 23:31

Essayer ce code

Sub EnvoyerCourrielNouveauCandidat()
            
    Dim OlkApp As Object
    
      
    Set OlkApp = CreateObject("Outlook.Application")

    With OlkApp.CreateItem(0)
        .Subject = "Séquence d'embauche - Nouveau candidat ajouté"
        .To =
        .HTMLBody = "Bonjour" & ",<br><br>" & _
                    "Un candidat a été ajouté :" & "<br><br>" & _
                    Me.FrmCandidats.tbxNom.Value & "<br><br>" & _
                    "Belle journée!"
                    
        .Send
    End With
    Range("E6").Select

End Sub
0
Jenni > thev Messages postés 1853 Date d'inscription lundi 7 avril 2008 Statut Membre Dernière intervention 27 avril 2024
25 mai 2023 à 01:45

J'ai du mettre Me.FrmCandidats.tbxNom.Value entre guillemet sinon ça indiquait Utilisation incorrecte du mot clé Me et voici le résultat :

Bonjour,

Un candidat a été ajouté :

Me.FrmCandidats.tbxNom.Value

Belle journée!

0
thev Messages postés 1853 Date d'inscription lundi 7 avril 2008 Statut Membre Dernière intervention 27 avril 2024 681 > Jenni
25 mai 2023 à 08:51

Mettez votre procédure EnvoyerCourrier dans le code de votre UserForm ou remplacez Me par le nom de votre UserForm.

0