Error 1004 Sort method Range class failed. Que faire ?

Bonjour,

Dans le cadre d'une formation Excel, je suis actuellement sur les macros.

Je suis sous Excel 365 version anglophone et apparemment le professeur en podcast sous Excel 2013.

Voici le code initial du VBA :

Sub DivisionSort()

'

' Sort List by Division Ascending

'

'

Selection.Sort Key1:=Range("A4"), Order1:=xlAscending, Header:=xlGuess, _

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _

DataOption1:=xlSortNormal

End Sub

Sub CategorySort()

'

' Sort List by Category Ascending

'

'

Selection.Sort Key1:=Range("B4"), Order1:=xlAscending, Header:=xlGuess, _

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _

DataOption1:=xlSortNormal

End Sub

Sub TotalSort()

'

' Sort List by Total Sales Ascending

'

'

Selection.Sort Key1:=Range("F4"), Order1:=xlAscending, Header:=xlGuess, _

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _

DataOption1:=xlSortNormal

End Sub

Après rajout exact du code demandé par le professeur, voici ce qui apparait en pour générer la boite de choix de sélection :

Public Sub UserSortInput()

Dim UserInput As String

Dim promptMSG As String

promptMSG = "Enter a numeric value to sort..." & vbCrLf & _

"1 --- Sort by Division" & vbCrLf & _

"2 --- Sort by Category" & vbCrLf & _

"3 --- Sort by Total"

UserInput = InputBox(promptMSG)

If UserInput = "1" Then

DivisionSort

ElseIf UserInput = "2" Then

CategorySort

ElseIf UserInput = "3" Then

TotalSort

End If

À chaque fois j'ai des messages d'erreur différents, Break mode ou Error 1004.

Sauriez vous m'en dire plus ? Je tourne en rond depuis 1 semaine à cause de ce problème.

Merci d,avance pour votre aide et bonne journée

Louis

bonjour,

ces macros trient la selection active sur la feuille. vérifie que tu as bien sélectionné les données à trier avant de lancer la macro.

Bonjour h2s04 et merci pour la réponse.

Les données à trier ont été définies ainsi :

Public Sub UserSortInput()

Dim UserInput As String

Dim promptMSG As String

promptMSG = "Enter a numeric value to sort..." & vbCrLf & _

"1 --- Sort by Division" & vbCrLf & _

"2 --- Sort by Category" & vbCrLf & _

"3 --- Sort by Total"

UserInput = InputBox(promptMSG)

If UserInput = "1" Then

DivisionSort

ElseIf UserInput = "2" Then

CategorySort

ElseIf UserInput = "3" Then

TotalSort

End If

End Sub

Les 3 catégories existent dans le code initial.

Je me demandais donc si le problème venait des différentes versions d'Excel ou d'un élément de la formule, de parenthèse, "sub" à rajouter, parenthèses à ouvrir, fermer.

Je suis désolé je débute totalement en macro et c'est la première fois que je dois entrer une formule "en code source".

Merci beaucoup , je regarde de mon côté via différents tutoriels mais dans le tutoriel Udemy, la formule fonctionne mais pas dans mon Excel,

Voilà donc pourquoi je demande de l'aide mais je vais explorer ta suggestion.

Bonjour,

si tu as besoin d'aide supplémentaire, merci de mettre ton classeur.

Bonjour h2so4,

Effectivement avec le classeur cela sera plus concret.

Le voici donc en pièce jointe et à tout hasard le même en format txt.

Il s'agit d'un exercice donc rien de confidentiel, mais vraiment j'aimerais comprendre

Merci d'avance pour l'aide.

Bonne journée

Louis

Bonjour,

pas de problème avec tes macros, si le tableau est sélectionné avant de les lancer.

Si tu veux que la macro sélectionne le tableau pour toi, adapte ton code ainsi :

Public Sub UserSortInput()
    Dim UserInput As String
    Dim promptMSG As String

    Range("A3").CurrentRegion.Select

    promptMSG = "Enter a numeric value to sort..." & vbCrLf & _
          "1 --- Sort by Division" & vbCrLf & _
          "2 --- Sort by  Category" & vbCrLf & _
          "3 --- Sort by Total"

    UserInput = InputBox(promptMSG)

    If UserInput = "1" Then
                    DivisionSort
    ElseIf UserInput = "2" Then
                    CategorySort
    ElseIf UserInput = "3" Then
                    TotalSort
    End If

End Sub

Re bonjour,

Oui ça fonctionne comme ça merci, même en créant un bouton

Merci !

Rechercher des sujets similaires à "error 1004 sort method range class failed que"