Richard 58
XLDnaute Nouveau
Bonjour,
Une erreur est présent dans mon code, je n'arrive pas à la résoudre.
Je ne maitrise pas du tout le VBA, à chaque fois que je fais un fichier je suis dans l'approximation et le tâtonnement. J'ai donc besoin de vous.
	
	
	
	
	
		
	
		
			
		
		
	
				
			Une erreur est présent dans mon code, je n'arrive pas à la résoudre.
Je ne maitrise pas du tout le VBA, à chaque fois que je fais un fichier je suis dans l'approximation et le tâtonnement. J'ai donc besoin de vous.
		VB:
	
	
	Option Explicit
Dim f As Worksheets, cell As Range
Dim no_ligne&, n&, ln&, i&, j&, flag&
'Pour le bouton Créer
Private Sub CommandButton1_Click()
    Set cell = f.Range("A:B").Find(TextBox1, TextBox2, lookat:=xlWhole)
    If Not cell Is Nothing Then
        MsgBox "L'adhérent " & TextBox1 & " existe déjà. Vous ne pouvez que la modifier.", 16
        Exit Sub
    End If
    If MsgBox("Confirmez-vous l’insertion de cet adhérent ?", vbYesNo, "Demande de confirmation d’ajout") = vbYes Then
        ln = f.Range("a65536").End(xlUp).Row + 1 'Pour placer le nouvel enregistrement à la première ligne de tableau non vide
        For j = 1 To 50
        Select Case j
        Case 1, 2, 4 To 6, 8 To 12, 15 To 28, 41 To 45, 49
            f.Cells(ln, j) = Controls("TextBox" & j).Value
        Case 3, 7, 39, 40, 50
            f.Cells(ln, j) = Controls("ComboBox" & j).Value
        Case 13, 14, 29 To 38, 46, 47
            f.Cells(ln, j) = Controls("CheckBox" & j).Value
        End Select
        
        Next j
        f.Range(f.Cells(3, 1), f.Cells(f.Range("A" & Rows.Count).End(xlUp).Row, f.Cells(2, Columns.Count).End(xlToLeft).Column)).Sort _
                key1:=f.Range("A3"), order1:=xlAscending, Header:=xlNo
    Else
        Exit Sub
    End If
    MsgBox "L'adhérent''" & TextBox1 & " a été créée."
    Unload Me
    UserForm1.Show
End Sub
'pour le bouton supprimer
Private Sub CommandButton2_Click()
    
    If TextBox1 = "" Then Exit Sub
    If MsgBox("Confirmez-vous la suppression de la fiche " & TextBox1 & " ?", vbYesNo, "La suppression a été prise en compte.") = vbYes Then
        With Sheets("Adhérent")
            ln = .Range("A3:A" & .Range("A" & Rows.Count).End(xlUp).Row).Find(TextBox1, lookat:=xlWhole).Row
            .Rows(ln & ":" & ln).Delete Shift:=xlUp
        End With
    Else
        Exit Sub
    End If
    MsgBox "La fiche " & TextBox1 & " a été supprimée."
    Unload Me
    UserForm1.Show
End Sub
'Pour le bouton Quitter
Private Sub CommandButton3_Click()
    Unload Me
End Sub
'pour le bouton Modifier
Private Sub CommandButton4_Click()
    
    If TextBox1 = "" Then Exit Sub
    If MsgBox("Confirmez-vous la modification de la fiche " & TextBox1 & " ?", vbYesNo, "La modification a été prise en compte.") = vbYes Then
        ln = f.Range("A3:A" & f.Range("A" & Rows.Count).End(xlUp).Row).Find(ComboBox1, lookat:=xlWhole).Row
        flag = 1
       For j = 1 To 50
        Select Case j
        Case 1, 2, 4 To 6, 8 To 12, 15 To 28, 41 To 45, 49
            f.Cells(ln, j) = Controls("TextBox" & j).Value
        Case 3, 7, 39, 40, 50
            f.Cells(ln, j) = Controls("ComboBox" & j).Value
        Case 13, 14, 29 To 38, 46, 47
            f.Cells(ln, j) = Controls("CheckBox" & j).Value
        End Select
        Next j
        flag = 0
    End If
    MsgBox "La fiche " & TextBox1 & " a été modifiée."
    Unload Me
    UserForm1.Show
End Sub
'Pour rechercher
Private Sub ComboBox1_Change()
    If flag = 1 Then Exit Sub
    If ComboBox1 = "" Then Exit Sub
    If Not ComboBox1.Value = "" Then
        no_ligne = ComboBox1.ListIndex + 3
        For n = 1 To 50
            Controls("TextBox" & n) = f.Cells(no_ligne, n).Value
        Next n
    Else
        MsgBox "Vous devez choisir un nom.", 16
        Exit Sub
    End If
End Sub