Comparaison d'unede deux périodes à partir d'une même clé

momo

XLDnaute Occasionnel
Bonjour à tous,

Je voudrais demander une faveur

j'ai un fichier ou je fais des comparaisons sur deux années pour un même clé de recherche

lorsque les variation dépassent un certain seuil je voudrais m'expliquer le détail de ce qui cause cete variation. Ce détails se trouve dans un autre onglet.

Je voudrais donc que par VBA si possible, lorsque la variation dépasse le seuil pour une clé de recherche; ma macro puisse aller chercher les infos pour cette clé dans les onglets détails de tous les comptes et puisse faire une variation

je joins un fichier afin de mieux expliquer ma demande


je vous remercie par avance et bonne journée dominicale
 

Pièces jointes

  • Comparaison à partir d'une même clé.xlsx
    19.4 KB · Affichages: 52
  • Comparaison à partir d'une même clé.xlsx
    19.4 KB · Affichages: 52

momo

XLDnaute Occasionnel
Re : Comparaison d'unede deux périodes à partir d'une même clé

Dans ce cas l'année ou le compte n'existe pas son montant par défaut sera 0

Si par exemple un compte existait l'année dernière et qu'au cours de l'année on l'a totalement apuré c normal qu'en fin d'année il n'apparaisse plus donc son montant par défaut est de 0 et vice versa
 

klin89

XLDnaute Accro
Re : Comparaison d'unede deux périodes à partir d'une même clé

Re à tous,:)

Pour faire simple, on peut entrer le N° de compte via une InputBox
Et afficher son résultat en feuil1.
A tester dans toutes les configurations.
VB:
Option Explicit

Sub test()
Dim a, i As Long, n As Long, y, e, w, s, NCompte As String
    NCompte = Application.InputBox("Choisir un n° de compte", , 551107110, Type:=2)
    a = Sheets("2014").Range("a1").CurrentRegion.Resize(, 3).Value
    With CreateObject("Scripting.Dictionary")
        .CompareMode = 1
        For i = 1 To UBound(a, 1)
            a(i, 1) = CStr(a(i, 1)): a(i, 2) = CStr(a(i, 2))
            If Not .exists(a(i, 1)) Then
                Set .Item(a(i, 1)) = _
                CreateObject("Scripting.Dictionary")
                .Item(a(i, 1)).CompareMode = 1
            End If
            .Item(a(i, 1))(a(i, 2)) = _
            VBA.Array(a(i, 1), a(i, 2), a(i, 3), Empty, Empty)
        Next
        a = Sheets("2015").Range("a1").CurrentRegion.Resize(, 3).Value
        For i = 1 To UBound(a, 1)
            a(i, 1) = CStr(a(i, 1)): a(i, 2) = CStr(a(i, 2))
            If Not .exists(a(i, 1)) Then
                Set .Item(a(i, 1)) = _
                CreateObject("Scripting.Dictionary")
                .Item(a(i, 1)).CompareMode = 1
            End If
            If Not .Item(a(i, 1)).exists(a(i, 2)) Then
                .Item(a(i, 1))(a(i, 2)) = _
                VBA.Array(a(i, 1), a(i, 2), Empty, a(i, 3), Empty)
            Else
                w = .Item(a(i, 1))(a(i, 2))
                w(3) = a(i, 3)
                .Item(a(i, 1))(a(i, 2)) = w
            End If
        Next
        For Each e In .keys
            If e <> NCompte Then
                .Remove e
            Else
                For Each s In .Item(e).keys
                    w = .Item(e)(s)
                    w(4) = w(3) - w(2)
                    .Item(e)(s) = w
                Next
            End If
        Next
        If .Count > 0 Then
            y = .items
        Else
            MsgBox "Ce compte n'existe pas": Exit Sub
        End If
    End With
    'Restitution et mise en forme
    Application.ScreenUpdating = False
    With Sheets("Feuil1")
        .Cells.Clear
        .Columns("b").NumberFormat = "@"
        With .Cells(1)
            .Resize(1, 5).Value = Array("Comptes", "Account", "2014", "2015", "Variation")
            With .Offset(1).Resize(y(0).Count, 5)
                .Value = _
                Application.Transpose(Application.Transpose(y(0).items))
            End With
            With .CurrentRegion
                .Font.Name = "calibri"
                .Font.Size = 10
                .VerticalAlignment = xlCenter
                .BorderAround Weight:=xlThin
                .Borders(xlInsideVertical).Weight = xlThin
                With .Offset(1).Resize(.Rows.Count - 1)
                    .Columns("c:e").NumberFormat = _
                    "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
                End With
                With .Rows(1)
                    .BorderAround Weight:=xlThin
                    .Interior.ColorIndex = 38
                    .HorizontalAlignment = xlCenter
                End With
                .Columns.AutoFit
            End With
        End With
        .Activate
    End With
    Application.ScreenUpdating = True
End Sub
Ça sent l'usine à gaz :p
klin89
 

momo

XLDnaute Occasionnel
Re : Comparaison d'unede deux périodes à partir d'une même clé

Bonjour Klin89; Bonjour Sousou

@Klin89 Mais noooon ça ne sent pas pas du tout l'usine à gase la preuve votre proposition est parfaite et il n'y a rien a dire... ça a même pris enc compte la dernière conversation avec Sousou... franchement c'est du Bon

@Sousou n'empeche que je veux bien qu'on aille jusqu'au bout de ce qu'on a commencé. On a tellement ramé pour s'arrêter en si bon chemin.

Mais franchement ca dépend de vous moi je resterai en attente comme vous l'aviez promis.. Pour finir la mise en forme, concurrencer Kiln sur la rapidité (Je rigole) et tenir compte de la dernière conversation avec vous pour les comptes n'existant plus etc;;
 
Dernière édition:

momo

XLDnaute Occasionnel
Re : Comparaison d'unede deux périodes à partir d'une même clé

@ Sousou

Je n'avais pas vu votre post avant d'envoyer le mien....

C'est parfait mtn et la rapidité d'exécution y est Franchement merci beaucoup... Ne reste plus que la mise en forme
 

momo

XLDnaute Occasionnel
Re : Comparaison d'unede deux périodes à partir d'une même clé

Bonjour Klin,

Je voudrais solliciter votre aide sur la macro que vous m'avez aidé a faire

Ci joint une feuille excel qui traduit mieux ma demande
 

Pièces jointes

  • Aides supp.xlsm
    40.9 KB · Affichages: 35

klin89

XLDnaute Accro
Re : Comparaison d'unede deux périodes à partir d'une même clé

Re momo :)

A tester avec le fichier du post #36
VB:
Option Explicit

Sub test()
Dim a, w(), i As Long, n As Long, j As Byte, y, x, e, s
    a = Sheets("2013").Range("a1").CurrentRegion.Value
    With CreateObject("Scripting.Dictionary")
        .CompareMode = 1
        For i = 1 To UBound(a, 1)
            If Not .exists(a(i, 1)) Then
                Set .Item(a(i, 1)) = _
                CreateObject("Scripting.Dictionary")
                .Item(a(i, 1)).CompareMode = 1
            End If
            .Item(a(i, 1))(a(i, 2)) = _
            VBA.Array(a(i, 1), a(i, 2), a(i, 3), Empty, Empty, Empty, Empty)
        Next
        a = Sheets("2012").Range("a1").CurrentRegion.Value
        For i = 1 To UBound(a, 1)
            If Not .exists(a(i, 1)) Then
                Set .Item(a(i, 1)) = _
                CreateObject("Scripting.Dictionary")
                .Item(a(i, 1)).CompareMode = 1
            End If
            If Not .Item(a(i, 1)).exists(a(i, 2)) Then
                .Item(a(i, 1))(a(i, 2)) = _
                VBA.Array(a(i, 1), a(i, 2), Empty, a(i, 3), Empty, Empty, Empty)
            Else
                w = .Item(a(i, 1))(a(i, 2))
                w(3) = a(i, 3)
                .Item(a(i, 1))(a(i, 2)) = w
            End If
        Next
        For Each e In .keys
            For Each s In .Item(e).keys
                w = .Item(e)(s)
                w(4) = w(2) - w(3)
                If w(3) = 0 Then w(5) = "infini" Else w(5) = w(4) / w(3)
                .Item(e)(s) = w
            Next
        Next
        x = .keys: y = .items
    End With
    'Restitution et mise en forme
    Application.ScreenUpdating = False
    For i = 0 To UBound(x)
        On Error Resume Next
        Application.DisplayAlerts = False
        Sheets(x(i)).Delete
        On Error GoTo 0
        Sheets.Add(after:=Sheets(Sheets.Count)).Name = x(i)
        n = 0
        With Sheets(x(i))
            With .Cells(1)
                .Resize(1, 7).Value = Array("Comptes", "Account", "31/12/2013", "31/12/2012", "Variations", "", "Notes")
                .Offset(1).Resize(1, 7).Value = Array("", "", "", "", "Valeur", "Pourcentage", "")
                With .Offset(2).Resize(y(i).Count, 7)
                    .Value = _
                    Application.Transpose(Application.Transpose(y(i).items))
                    n = n + .Rows.Count + 2
                End With
                With .Offset(n).Resize(, 7)
                    .Value = Array("", x(i), _
                                   "=sum(r3c:r[-1]c)", _
                                   "=sum(r3c:r[-1]c)", _
                                   "=rc[-2]-rc[-1]", _
                                   "=IF(rc[-2]>0,rc[-1]/rc[-2],""infini"")", "")
                    .BorderAround Weight:=xlThin
                    With .Offset(, 1).Resize(, .Columns.Count - 1)
                        .Interior.ColorIndex = 40
                    End With
                End With
                With .CurrentRegion
                    .Font.Name = "calibri"
                    .Font.Size = 10
                    .VerticalAlignment = xlCenter
                    .BorderAround Weight:=xlThin
                    .Borders(xlInsideVertical).Weight = xlThin
                    .Columns("c:e").Offset(2).Resize(.Rows.Count - 2).NumberFormat = "#,##0,"
                    .Columns("f").Offset(2).Resize(.Rows.Count - 2).NumberFormat = "0.00%"
                    '.Columns.AutoFit
                    .Columns.ColumnWidth = Array(16, 24, 12, 12, 12, 12, 21)
                    With .Rows("1:2")
                        .BorderAround Weight:=xlThin
                        .Interior.ColorIndex = 36
                        .HorizontalAlignment = xlCenter
                    End With
                    For j = 1 To 4
                        .Cells(1, j).Resize(2).MergeCells = True
                    Next
                    .Cells(1, 7).Resize(2).MergeCells = True
                    .Cells(1, 5).Resize(, 2).HorizontalAlignment = xlCenterAcrossSelection
                    .Cells(2, 5).Resize(, 2).Borders(xlEdgeTop).Weight = xlThin
                End With
            End With
        End With
    Next
    Application.ScreenUpdating = True
End Sub
klin89
 

Lone-wolf

XLDnaute Barbatruc
Re : Comparaison d'unede deux périodes à partir d'une même clé

Bonsoir à tous :)

@klin: mieux vaut mettre ceci au début, avant a = Sheets("2013").Range("a1").CurrentRegion.Value

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Sans mettre Application.ScreenUpdating = True, à cause de toutes ces boucles.
 

Discussions similaires

Statistiques des forums

Discussions
312 505
Messages
2 089 066
Membres
104 015
dernier inscrit
kkgk