Advertisement
Brekekekeke

Untitled

Jan 29th, 2023 (edited)
1,751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ' V0.3.3
  3. Sub ATransposeTable()
  4.     Dim margeGauche As Integer
  5.     Dim margeHaut As Integer
  6.     Dim ecartMatricesGenerees
  7.     Dim ecart12 As Integer
  8.     Dim ecartMoyenneMediane As Integer
  9.     Dim espaceTitreTableau As Integer
  10.     Dim ecartEchelle As Integer
  11.     Dim selectedRange As Range
  12.     Set selectedRange = selection
  13.    
  14.     ' //////////Réglage des écartements :\\\\\\\\\\
  15.    ' Marge générale à gauche
  16.    margeGauche = 1
  17.     ' Marge générale en haut
  18.    margeHaut = 1
  19.     ' Ecartement horizontal des matrices renversées
  20.    ecartMatricesGenerees = 2
  21.     ' Ecart horizontal entre les matrices avec et sans chiffres
  22.    ecart12 = 1
  23.     'Ecart horizontal entre les blocs Mediane et Moyenne
  24.    ecartMoyenneMediane = 6
  25.     ' Marge verticale des séries de matrices
  26.    espaceTitreTableau = 3
  27.     ' Marge horizontale entre matrice invisible et échelle
  28.    ecartEchelle = 1
  29.    
  30.     ' //////////Enregistrement des références :\\\\\\\\\\
  31.    Dim firstRowSize As Integer
  32.     Dim indexFirstRow As Integer
  33.     Dim indexLastRow As Integer
  34.    
  35.     Dim firstColSize As Integer
  36.     Dim indexFirstCol As Integer
  37.     Dim indexLastCol As Integer
  38.    
  39.     firstRowSize = selectedRange.Rows.Count
  40.     indexFirstRow = selectedRange.Cells(0, 0).Row + 1
  41.     indexLastRow = indexFirstRow + firstRowSize
  42.     firstColSize = selectedRange.Columns.Count
  43.     indexFirstCol = selectedRange.Cells(0, 0).Column + 1
  44.     indexLastCol = indexFirstCol + firstColSize
  45.    
  46.    
  47.     ' //////////Renversement des matrices :\\\\\\\\\\
  48.    
  49.     ' Remplace les cases vides par NaN dans la matrice initiale
  50.    Dim cell As Range
  51.     For Each cell In selectedRange
  52.         If cell.Value = "" Then
  53.             cell.Value = "NaN"
  54.         End If
  55.     Next cell
  56.    
  57.     ' Transpose la plage de données sélectionnée
  58.    Dim transposedRange As Range
  59.     Set transposedRange = ActiveSheet.Cells(indexFirstRow, indexLastCol + ecartMatricesGenerees).Resize(firstColSize, firstRowSize)
  60.     ' Boucle à travers les cellules de la plage originale
  61.    For i = 1 To firstRowSize
  62.         For j = 1 To firstColSize
  63.             ' Copiez la valeur de la cellule originale dans la cellule transposée correspondante
  64.            transposedRange.Cells(j, i).Value = selectedRange.Cells(i, j).Value
  65.         Next j
  66.     Next i
  67.    
  68.     ' Inverser l'ordre des lignes
  69.    Dim invertedRange As Range
  70.     Set invertedRange = transposedRange.Offset(0, firstRowSize + ecartMatricesGenerees)
  71.     For i = 1 To transposedRange.Rows.Count
  72.         For j = 1 To transposedRange.Columns.Count
  73.                 invertedRange.Cells(i, j).Value = transposedRange.Cells(transposedRange.Rows.Count + 1 - i, j).Value
  74.         Next j
  75.     Next i
  76.    
  77.    
  78.     ' //////////Création des matrices de présentation :\\\\\\\\\\
  79.    
  80.     ' Détermine où commencer les matrices selon les dimensions de la map
  81.    Dim rowSize As Integer
  82.     Dim colSize As Integer
  83.     Dim indexFirstPresRow As Integer
  84.     rowSize = firstColSize
  85.     colSize = firstRowSize
  86.     If firstColSize > firstRowSize Then
  87.         indexFirstPresRow = margeHaut + firstColSize + espaceTitreTableau + 1
  88.     Else
  89.         indexFirstPresRow = margeHaut + firstRowSize + espaceTitreTableau + 1
  90.     End If
  91.     Dim scaleColSize As Integer
  92.     scaleColSize = 2
  93.  
  94.     ' Valeurs brutes
  95.    Dim bruteMediane1 As Range
  96.     Set bruteMediane1 = ActiveSheet.Cells(indexFirstPresRow, indexFirstCol).Resize(rowSize, colSize)
  97.     bruteMediane1.BorderAround _
  98.         ColorIndex:=xlAutomatic, Weight:=xlThin
  99.     For i = 1 To rowSize
  100.         For j = 1 To colSize
  101.                 bruteMediane1.Cells(i, j).Value = invertedRange.Cells(i, j).Value
  102.                 bruteMediane1.Cells(i, j).HorizontalAlignment = xlCenter
  103.                 bruteMediane1.Cells(i, j).VerticalAlignment = xlCenter
  104.         Next j
  105.     Next i
  106.    
  107.    
  108.     Dim bruteMediane2 As Range
  109.     Set bruteMediane2 = bruteMediane1.Offset(0, colSize + ecart12)
  110.     bruteMediane2.BorderAround _
  111.         ColorIndex:=xlAutomatic, Weight:=xlThin
  112.     For i = 1 To rowSize
  113.         For j = 1 To colSize
  114.                 bruteMediane2.Cells(i, j).Value = invertedRange.Cells(i, j).Value
  115.                 bruteMediane2.Cells(i, j).HorizontalAlignment = xlCenter
  116.                 bruteMediane2.Cells(i, j).VerticalAlignment = xlCenter
  117.                 bruteMediane2.Cells(i, j).NumberFormat = ";;;"
  118.         Next j
  119.         bruteMediane2.Cells(i, j + ecartEchelle).NumberFormat = ";;;"
  120.     Next i
  121.     Dim zoneEchelleBruteMediane As Range
  122.     Set zoneEchelleBruteMediane = ActiveSheet.Cells(indexFirstPresRow + 1, indexFirstCol + colSize + ecart12 + colSize + 1 + 1).Resize(rowSize, scaleColSize)
  123.     Call GenererEchelle(zoneEchelleBruteMediane)
  124.     Dim zoneAColorer As Range
  125.     Set zoneAColorer = ActiveSheet.Cells(indexFirstPresRow, indexFirstCol).Resize(rowSize, colSize + ecart12 + colSize + ecartEchelle + 1)
  126.     Call ColorerMatrices(zoneAColorer, "med")
  127.    
  128.     Dim bruteMoyenne1 As Range
  129.     Set bruteMoyenne1 = bruteMediane2.Offset(0, colSize + ecartMoyenneMediane)
  130.     bruteMoyenne1.BorderAround _
  131.         ColorIndex:=xlAutomatic, Weight:=xlThin
  132.     For i = 1 To rowSize
  133.         For j = 1 To colSize
  134.                 bruteMoyenne1.Cells(i, j).Value = invertedRange.Cells(i, j).Value
  135.                 bruteMoyenne1.Cells(i, j).HorizontalAlignment = xlCenter
  136.                 bruteMoyenne1.Cells(i, j).VerticalAlignment = xlCenter
  137.                
  138.         Next j
  139.     Next i
  140.    
  141.     Dim bruteMoyenne2 As Range
  142.     Set bruteMoyenne2 = bruteMoyenne1.Offset(0, colSize + ecart12)
  143.     bruteMoyenne2.BorderAround _
  144.         ColorIndex:=xlAutomatic, Weight:=xlThin
  145.     For i = 1 To rowSize
  146.         For j = 1 To colSize
  147.                 bruteMoyenne2.Cells(i, j).Value = invertedRange.Cells(i, j).Value
  148.                 bruteMoyenne2.Cells(i, j).HorizontalAlignment = xlCenter
  149.                 bruteMoyenne2.Cells(i, j).VerticalAlignment = xlCenter
  150.                 bruteMoyenne2.Cells(i, j).NumberFormat = ";;;"
  151.                
  152.         Next j
  153.         bruteMoyenne2.Cells(i, j + ecartEchelle).NumberFormat = ";;;"
  154.     Next i
  155.     Dim zoneEchelleBruteMoyenne As Range
  156.     Set zoneEchelleBruteMoyenne = ActiveSheet.Cells(indexFirstPresRow + 1, indexFirstCol + colSize + ecart12 + colSize + ecartMoyenneMediane + colSize + ecart12 + colSize + 1 + 1).Resize(rowSize, scaleColSize)
  157.     Call GenererEchelle(zoneEchelleBruteMoyenne)
  158.     Call ColorerMatrices(zoneAColorer.Offset(0, colSize + ecart12 + colSize + ecartMoyenneMediane), "moy")
  159.    
  160.     ' Valeurs modifiées
  161.    Dim reponse As Integer
  162.     reponse = MsgBox("Ecraser les matrices modifiées ?", vbYesNo)
  163.     If reponse = vbYes Then
  164.         Dim modifMediane1 As Range
  165.         Set modifMediane1 = bruteMediane1.Offset(rowSize + espaceTitreTableau, 0)
  166.         modifMediane1.BorderAround _
  167.         ColorIndex:=xlAutomatic, Weight:=xlThin
  168.         For i = 1 To rowSize
  169.             For j = 1 To colSize
  170.                     modifMediane1.Cells(i, j).Value = invertedRange.Cells(i, j).Value
  171.                     modifMediane1.Cells(i, j).HorizontalAlignment = xlCenter
  172.                     modifMediane1.Cells(i, j).VerticalAlignment = xlCenter
  173.             Next j
  174.         Next i
  175.        
  176.         Dim modifMediane2 As Range
  177.         Set modifMediane2 = bruteMediane2.Offset(rowSize + espaceTitreTableau, 0)
  178.         modifMediane2.BorderAround _
  179.         ColorIndex:=xlAutomatic, Weight:=xlThin
  180.         For i = 1 To rowSize
  181.             For j = 1 To colSize
  182.                     modifMediane2.Cells(i, j).Value = invertedRange.Cells(i, j).Value
  183.                     modifMediane2.Cells(i, j).HorizontalAlignment = xlCenter
  184.                     modifMediane2.Cells(i, j).VerticalAlignment = xlCenter
  185.                     modifMediane2.Cells(i, j).NumberFormat = ";;;"
  186.             Next j
  187.             modifMediane2.Cells(i, j + ecartEchelle).NumberFormat = ";;;"
  188.         Next i
  189.         Call GenererEchelle(zoneEchelleBruteMediane.Offset(rowSize + espaceTitreTableau, 0))
  190.         Call ColorerMatrices(zoneAColorer.Offset(rowSize + espaceTitreTableau, 0), "med")
  191.        
  192.         Dim modifMoyenne1 As Range
  193.         Set modifMoyenne1 = bruteMoyenne1.Offset(rowSize + espaceTitreTableau, 0)
  194.         modifMoyenne1.BorderAround _
  195.         ColorIndex:=xlAutomatic, Weight:=xlThin
  196.         For i = 1 To rowSize
  197.             For j = 1 To colSize
  198.                     modifMoyenne1.Cells(i, j).Value = invertedRange.Cells(i, j).Value
  199.                     modifMoyenne1.Cells(i, j).HorizontalAlignment = xlCenter
  200.                     modifMoyenne1.Cells(i, j).VerticalAlignment = xlCenter
  201.             Next j
  202.         Next i
  203.        
  204.         Dim modifMoyenne2 As Range
  205.         Set modifMoyenne2 = bruteMoyenne2.Offset(rowSize + espaceTitreTableau, 0)
  206.         modifMoyenne2.BorderAround _
  207.         ColorIndex:=xlAutomatic, Weight:=xlThin
  208.         For i = 1 To rowSize
  209.             For j = 1 To colSize
  210.                     modifMoyenne2.Cells(i, j).Value = invertedRange.Cells(i, j).Value
  211.                     modifMoyenne2.Cells(i, j).HorizontalAlignment = xlCenter
  212.                     modifMoyenne2.Cells(i, j).VerticalAlignment = xlCenter
  213.                     modifMoyenne2.Cells(i, j).NumberFormat = ";;;"
  214.             Next j
  215.             modifMoyenne2.Cells(i, j + ecartEchelle).NumberFormat = ";;;"
  216.         Next i
  217.         Call GenererEchelle(zoneEchelleBruteMoyenne.Offset(rowSize + espaceTitreTableau, 0))
  218.         Call ColorerMatrices(zoneAColorer.Offset(rowSize + espaceTitreTableau, colSize + ecart12 + colSize + ecartMoyenneMediane), "moy")
  219.     End If
  220.    
  221. End Sub
  222.  
  223. Sub ExtraireValeurs()
  224.     'Déclaration des variables
  225.    Dim selectedRange As Range
  226.     Set selectedRange = selection
  227.     Dim cell As Range
  228.  
  229.     Dim ecriture As Range
  230.     Set ecriture = Application.InputBox("Selectionne la case d'écriture", Type:=8)
  231.     'Parcourir chaque cellule dans la sélection
  232.    Dim i As Integer
  233.     i = ecriture.Row
  234.     For Each cell In selectedRange
  235.         'Copier la valeur de la cellule dans la colonne I à partir de l'adresse I65
  236.        Cells(i, ecriture.Column).Value = cell.Value
  237.         i = i + 1
  238.     Next cell
  239. End Sub
  240.  
  241. Sub GenererEchelle(zoneEchelle As Range)
  242.     Dim maxEchelle As Integer
  243.     Dim minEchelle As Integer
  244.     Dim pasEchelle As Integer
  245.     Select Case Left(ActiveSheet.Name, 1)
  246.         Case "s", "S"
  247.             maxEchelle = 3000
  248.             minEchelle = 200
  249.             pasEchelle = 200
  250.         Case "y", "Y"
  251.             maxEchelle = 1000
  252.             minEchelle = 0
  253.             pasEchelle = 100
  254.         Case "l", "L"
  255.             maxEchelle = 1600
  256.             minEchelle = 100
  257.             pasEchelle = 100
  258.         Case Else
  259.             MsgBox "Le nom de l'onglet ne commence pas par 's', 'y' ou 'l'."
  260.     End Select
  261.     Dim echelleRowSize As Integer
  262.     echelleRowSize = (maxEchelle - minEchelle) / pasEchelle + 1
  263.    
  264.     Dim echelleCouleurs As Range
  265.     Set echelleCouleurs = zoneEchelle.Cells(0, 0).Resize(echelleRowSize, 1)
  266.    
  267.     echelleCouleurs.Borders(xlDiagonalDown).LineStyle = xlNone
  268.     echelleCouleurs.Borders(xlDiagonalUp).LineStyle = xlNone
  269.     With echelleCouleurs.Borders(xlEdgeLeft)
  270.         .LineStyle = xlContinuous
  271.         .ColorIndex = 0
  272.         .TintAndShade = 0
  273.         .Weight = xlThin
  274.     End With
  275.     With echelleCouleurs.Borders(xlEdgeTop)
  276.         .LineStyle = xlContinuous
  277.         .ColorIndex = 0
  278.         .TintAndShade = 0
  279.         .Weight = xlThin
  280.     End With
  281.     With echelleCouleurs.Borders(xlEdgeBottom)
  282.         .LineStyle = xlContinuous
  283.         .ColorIndex = 0
  284.         .TintAndShade = 0
  285.         .Weight = xlThin
  286.     End With
  287.     With echelleCouleurs.Borders(xlEdgeRight)
  288.         .LineStyle = xlContinuous
  289.         .ColorIndex = 0
  290.         .TintAndShade = 0
  291.         .Weight = xlThin
  292.     End With
  293.     echelleCouleurs.Borders(xlInsideVertical).LineStyle = xlNone
  294.     echelleCouleurs.Borders(xlInsideHorizontal).LineStyle = xlNone
  295.    
  296.     Dim cell As Range
  297.     For Each cell In echelleCouleurs
  298.         cell.Formula = "=" + cell.Offset(0, 1).Address
  299.     Next cell
  300.    
  301.    
  302.     Dim echelleValeurs As Range
  303.     Set echelleValeurs = zoneEchelle.Cells(0, 1).Resize(echelleRowSize, 1)
  304.     With echelleValeurs.Interior
  305.         .Pattern = xlSolid
  306.         .PatternColorIndex = xlAutomatic
  307.         .ThemeColor = xlThemeColorDark1
  308.         .TintAndShade = 0
  309.         .PatternTintAndShade = 0
  310.     End With
  311.     With echelleValeurs.Font
  312.         .Name = "Calibri"
  313.         .Size = 14
  314.         .Strikethrough = False
  315.         .Superscript = False
  316.         .Subscript = False
  317.         .OutlineFont = False
  318.         .Shadow = False
  319.         .Underline = xlUnderlineStyleNone
  320.         .ThemeColor = xlThemeColorLight1
  321.         .TintAndShade = 0
  322.         .ThemeFont = xlThemeFontMinor
  323.     End With
  324.     With echelleValeurs
  325.         .HorizontalAlignment = xlGeneral
  326.         .VerticalAlignment = xlCenter
  327.         .WrapText = False
  328.         .Orientation = 0
  329.         .AddIndent = False
  330.         .IndentLevel = 0
  331.         .ShrinkToFit = False
  332.         .ReadingOrder = xlContext
  333.         .MergeCells = False
  334.     End With
  335.     With echelleValeurs
  336.         .HorizontalAlignment = xlCenter
  337.         .VerticalAlignment = xlCenter
  338.         .WrapText = False
  339.         .Orientation = 0
  340.         .AddIndent = False
  341.         .IndentLevel = 0
  342.         .ShrinkToFit = False
  343.         .ReadingOrder = xlContext
  344.         .MergeCells = False
  345.     End With
  346.    
  347.     Dim currentEchelle As Integer
  348.     currentEchelle = maxEchelle
  349.     For Each cell In echelleValeurs
  350.         cell.Value = currentEchelle
  351.         currentEchelle = currentEchelle - pasEchelle
  352.     Next cell
  353.    
  354. End Sub
  355.  
  356. Sub ColorerMatrices(zoneAColorer As Range, medMoy As String)
  357.     Dim monType As XlConditionValueTypes
  358.     If medMoy = "med" Then
  359.         monType = xlConditionValuePercentile
  360.     ElseIf medMoy = "moy" Then
  361.         monType = xlConditionValuePercent
  362.     End If
  363.    
  364.     Dim themeColor1 As XlThemeColor
  365.     Dim themeColor2 As XlThemeColor
  366.     Dim themeColor3 As XlThemeColor
  367.     Dim tintShade1 As Double
  368.     Dim tintShade2 As Double
  369.     Dim tintShade3 As Double
  370.    
  371.     Select Case Left(ActiveSheet.Name, 1)
  372.         Case "s", "S"
  373.             themeColor1 = xlThemeColorAccent1
  374.             themeColor2 = xlThemeColorAccent4
  375.             themeColor3 = xlThemeColorAccent2
  376.             tintShade1 = 0.399975585192419
  377.             tintShade2 = 0.799981688894314
  378.             tintShade3 = 0
  379.         Case "y", "Y"
  380.             themeColor1 = 10066176
  381.             themeColor2 = xlThemeColorAccent4
  382.             themeColor3 = 13260
  383.             tintShade1 = 0
  384.             tintShade2 = 0.599993896298105
  385.             tintShade3 = 0
  386.         Case "l", "L"
  387.             themeColor1 = xlThemeColorDark1
  388.             themeColor2 = xlThemeColorAccent4
  389.             themeColor3 = xlThemeColorAccent2
  390.             tintShade1 = -0.499984740745262
  391.             tintShade2 = 0.799981688894314
  392.             tintShade3 = 0
  393.         Case Else
  394.             MsgBox "Le nom de l'onglet ne commence pas par 's', 'y' ou 'l'."
  395.     End Select
  396.    
  397.     zoneAColorer.FormatConditions.AddColorScale ColorScaleType:=3
  398.     zoneAColorer.FormatConditions(zoneAColorer.FormatConditions.Count).SetFirstPriority
  399.     zoneAColorer.FormatConditions(1).colorScaleCriteria(1).Type = _
  400.         xlConditionValueLowestValue
  401.     Select Case Left(ActiveSheet.Name, 1)
  402.         Case "s", "S", "l", "L"
  403.             With zoneAColorer.FormatConditions(1).colorScaleCriteria(1).FormatColor
  404.                 .ThemeColor = themeColor1
  405.                 .TintAndShade = tintShade1
  406.             End With
  407.             zoneAColorer.FormatConditions(1).colorScaleCriteria(2).Type = _
  408.                 monType
  409.             zoneAColorer.FormatConditions(1).colorScaleCriteria(2).Value = 50
  410.             With zoneAColorer.FormatConditions(1).colorScaleCriteria(2).FormatColor
  411.                 .ThemeColor = themeColor2
  412.                 .TintAndShade = tintShade2
  413.             End With
  414.             zoneAColorer.FormatConditions(1).colorScaleCriteria(3).Type = _
  415.                 xlConditionValueHighestValue
  416.             With zoneAColorer.FormatConditions(1).colorScaleCriteria(3).FormatColor
  417.                 .ThemeColor = themeColor3
  418.                 .TintAndShade = tintShade3
  419.             End With
  420.         Case "y", "Y"
  421.             With zoneAColorer.FormatConditions(1).colorScaleCriteria(1).FormatColor
  422.                     .Color = themeColor1
  423.                     .TintAndShade = tintShade1
  424.                 End With
  425.                 zoneAColorer.FormatConditions(1).colorScaleCriteria(2).Type = _
  426.                     monType
  427.                 zoneAColorer.FormatConditions(1).colorScaleCriteria(2).Value = 50
  428.                 With zoneAColorer.FormatConditions(1).colorScaleCriteria(2).FormatColor
  429.                     .ThemeColor = themeColor2
  430.                     .TintAndShade = tintShade2
  431.                 End With
  432.                 zoneAColorer.FormatConditions(1).colorScaleCriteria(3).Type = _
  433.                     xlConditionValueHighestValue
  434.                 With zoneAColorer.FormatConditions(1).colorScaleCriteria(3).FormatColor
  435.                     .Color = themeColor3
  436.                     .TintAndShade = tintShade3
  437.                 End With
  438.     End Select
  439. End Sub
  440.  
  441.  
  442. ' //////////Changelog :\\\\\\\\\\
  443. ' V0.2 : Encadre les matrices de présentation
  444. ' V0.2.1 : Centre verticalement les valeurs des matrices de presentation
  445. ' V0.3 : Nettoyage du code de placement des matrices de présentation; Ajout des échelles et coloration des matrices
  446. ' V0.3.1 : Gère la coloration différenciée entre médiane et moyenne; Gere des couleurs différentes selon que l'onglet commence par "l" ou par "s"
  447. ' V0.3.2 : Gère la coloration pour l'onglet "y"
  448. ' V0.3.3 : Change min max et pas sur l'échelle selon onglet
  449.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement