Souci dans la rédaction du macro

Hugues

XLDnaute Impliqué
Bonjour

Ci-joint un extrait d'une commande.
Je souhaiterais que cette commande fonctionne, au cas où l'utilisateur modifie le nom de l'onglet.
Pour cela, j'extrait le nom de l'onglet que je récupère dans le reste de la macro commande.

Mais cela ne fonctionne pas.

Pourriez vous m'indiquer le pourquoi de ce dysfonctionnement ?

Merci par avance,

Cordialement,

Hugues
 

Pièces jointes

  • erreur sur macro tri de données.xlsm
    51.9 KB · Affichages: 44

ROGER2327

XLDnaute Barbatruc
Re : Souci dans la rédaction du macro

Bonjour Hugues, Marc L.


Une version allégée possible :
Code:
Private Sub CommandButton1_Click()

    With Me 'fait référence à la feuille contenant le code, indépendamment
    'de son nom d'onglet.

' Détermination du nom de la feuille

        MsgBox ("Le nom de la feuille : " & .CodeName)

' Détermination du nom de l'onglet

        MsgBox ("Le nom de l'onglet : " & .Name)

' Tri des données en fonction du N° de compte et de la section analytique

        With .Sort
            With .SortFields
                .Clear
                .Add Key:=.Parent.Parent.Range("A2:A5000"), SortOn:=xlSortOnValues, _
                    Order:=xlAscending, DataOption:=xlSortTextAsNumbers
                .Add Key:=.Parent.Parent.Range("G2:G5000"), SortOn:=xlSortOnValues, _
                    Order:=xlAscending, DataOption:=xlSortNormal
            End With
            .SetRange .Parent.Range("A1:P5000")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

 ' Mise en forme des colonnes de dates
 
        .Range("D:E,O:O").NumberFormat = "dd/mm/yy;@"

 ' Mise en forme des colonnes des montants
 
        .Range("F:F,I:I,L:N").NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 ;_-* ""-""?? _€_-"

 ' Mise en forme du cadre
 
        DerLigne = .Cells(.Rows.Count, 1).End(xlUp).Row
        With .Range("A1:P" & DerLigne).Borders
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Item(xlEdgeLeft).Weight = xlThin
            .Item(xlEdgeTop).Weight = xlThin
            .Item(xlEdgeBottom).Weight = xlThick
            .Item(xlEdgeRight).Weight = xlThick
            .Item(xlInsideVertical).Weight = xlThin
            .Item(xlInsideHorizontal).Weight = xlHairline
        End With

' Mise en forme de l'entête des colonnes

        With .Range("A1:P1")
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
            With .Font
                .Name = "Tahoma"
                .FontStyle = "Gras"
                .Size = 8
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .Color = 10040064
                .TintAndShade = 0
                .ThemeFont = xlThemeFontNone
            End With


            With .Borders
                .LineStyle = xlContinuous
                .ColorIndex = xlAutomatic
                .TintAndShade = 0
                .Item(xlEdgeLeft).Weight = xlThin
                .Item(xlEdgeTop).Weight = xlThin
                .Item(xlEdgeBottom).Weight = xlMedium
                .Item(xlEdgeRight).Weight = xlThick
                .Item(xlInsideVertical).Weight = xlThin
                .Item(xlInsideHorizontal).LineStyle = xlNone
            End With
            With .Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 6750156
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        End With

'Figeage des volets

        .Cells(2, 2).Activate
        ActiveWindow.FreezePanes = True

    End With

End Sub


Bonne nuit.


ℝOGER2327
#8208


Samedi 28 Gueules 143 (Sainte Hylactor et Pamphagus - fête Suprême Quarte)
3 Ventôse An CCXXIV, 0,4063h - violier
2016-W08-1T00:58:30Z
 

ROGER2327

XLDnaute Barbatruc
Re : Souci dans la rédaction du macro

Bonjour Hugues, Marc L.


Une version allégée possible :
Code:
Private Sub CommandButton1_Click()

    With Me 'fait référence à la feuille contenant le code, indépendamment
    'de son nom propre et de son nom d'onglet.

' Détermination du nom de la feuille

        MsgBox ("Le nom de la feuille : " & .CodeName)

' Détermination du nom de l'onglet

        MsgBox ("Le nom de l'onglet : " & .Name)

' Tri des données en fonction du N° de compte et de la section analytique

        With .Sort
            With .SortFields
                .Clear
                .Add Key:=.Parent.Parent.Range("A2:A5000"), SortOn:=xlSortOnValues, _
                    Order:=xlAscending, DataOption:=xlSortTextAsNumbers
                .Add Key:=.Parent.Parent.Range("G2:G5000"), SortOn:=xlSortOnValues, _
                    Order:=xlAscending, DataOption:=xlSortNormal
            End With
            .SetRange .Parent.Range("A1:P5000")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

 ' Mise en forme des colonnes de dates
 
        .Range("D:E,O:O").NumberFormat = "dd/mm/yy;@"

 ' Mise en forme des colonnes des montants
 
        .Range("F:F,I:I,L:N").NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 ;_-* ""-""?? _€_-"

 ' Mise en forme du cadre
 
        DerLigne = .Cells(.Rows.Count, 1).End(xlUp).Row
        With .Range("A1:P" & DerLigne).Borders
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Item(xlEdgeLeft).Weight = xlThin
            .Item(xlEdgeTop).Weight = xlThin
            .Item(xlEdgeBottom).Weight = xlThick
            .Item(xlEdgeRight).Weight = xlThick
            .Item(xlInsideVertical).Weight = xlThin
            .Item(xlInsideHorizontal).Weight = xlHairline
        End With

' Mise en forme de l'entête des colonnes

        With .Range("A1:P1")
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
            With .Font
                .Name = "Tahoma"
                .FontStyle = "Gras"
                .Size = 8
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .Color = 10040064
                .TintAndShade = 0
                .ThemeFont = xlThemeFontNone
            End With


            With .Borders
                .LineStyle = xlContinuous
                .ColorIndex = xlAutomatic
                .TintAndShade = 0
                .Item(xlEdgeLeft).Weight = xlThin
                .Item(xlEdgeTop).Weight = xlThin
                .Item(xlEdgeBottom).Weight = xlMedium
                .Item(xlEdgeRight).Weight = xlThick
                .Item(xlInsideVertical).Weight = xlThin
                .Item(xlInsideHorizontal).LineStyle = xlNone
            End With
            With .Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 6750156
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        End With

'Figeage des volets

        .Cells(2, 2).Activate
        ActiveWindow.FreezePanes = True

    End With

End Sub


Bonne nuit.


ℝOGER2327
#8208


Samedi 28 Gueules 143 (Sainte Hylactor et Pamphagus - fête Suprême Quarte)
3 Ventôse An CCXXIV, 0,4063h - violier
2016-W08-1T00:58:30Z
 

Discussions similaires