Bonjour,
Je souhaiterai depuis un Userform, ajouter un client puis trier automatiquement les clients par ordre alphabétique et tout cela sans que ma feuille "Clients" soit active.

Lorsque je test le code, il m'est renvoyé "Erreur d'exécution '1004': Référence de tri non valide..."

Mon avant dernière ligne ".Apply" est sélectionnée.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Private Sub CommandButton2_Click()
Dim ligne As Integer
Dim flag As Integer
 
If TextBox1.Value = "" Or TextBox2.Value = "" Or TextBox3.Value = "" Or TextBox4.Value = "" Or TextBox5.Value = "" Or TextBox6.Value = "" Or TextBox7.Value = "" Then
    TextBox8.Value = "Donnée(s) Manquante(s)"
    Else
        ligne = 2
        flag = 0
        While Sheets("Clients").Cells(ligne, 1).Value <> ""
            If Sheets("Clients").Cells(ligne, 1).Value = Val(TextBox1.Value) Then
                TextBox8.Value = "Client déjà existant "
                flag = 1
            End If
        ligne = ligne + 1
        Wend
    If flag = 0 Then
    With Sheets("Clients")
        .Cells(ligne, 1).Value = TextBox1.Value
        .Cells(ligne, 2).Value = TextBox2.Value
        .Cells(ligne, 3).Value = TextBox3.Value
        .Cells(ligne, 4).Value = TextBox4.Value
        .Cells(ligne, 5).Value = TextBox5.Value
        .Cells(ligne, 6).Value = TextBox6.Value
        .Cells(ligne, 7).Value = TextBox7.Value
    End With
        TextBox1.Value = ""
        TextBox2.Value = ""
        TextBox3.Value = ""
        TextBox4.Value = ""
        TextBox5.Value = ""
        TextBox6.Value = ""
        TextBox7.Value = ""
        TextBox8.Value = ""
 
        'Trier
        Worksheets("Clients").Range("A1:G" & Trim(Str(ligne))).Select
        Worksheets("Clients").Sort.SortFields.Clear
        Worksheets("Clients").Sort.SortFields.Add Key:=Worksheets("Clients").Range("A1:G" & Trim(Str(ligne))) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
   With Worksheets("Clients").Sort
        .SetRange Worksheets("Clients").Range("A1:G" & Trim(Str(ligne)))
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
   End If
End If
 
End Sub
Désolé, le code est un peu long.

Si cela peut aider : c'est dans le userform14
Projet - Copie.xlsm