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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| Option Explicit
'Colonne P
Const colP = 16
'Définition de la ligne de départ
Const oLign = 3
Sub Decomposition()
'Déclaration des variables
Dim oRng As Range
Dim i As Integer, j As Integer, k As Integer
Dim oTableP() As String
Dim oTableQ() As String
Dim boolP As Boolean
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'Avec la feuille cible
With Worksheets("MaFeuil")
'Je set oRng sur la dernière ligne non vide de la colonne P
Set oRng = .Cells(WorksheetFunction.Max(.Cells(Rows.Count, colP).End(xlUp).Row, .Cells(Rows.Count, colP + 1).End(xlUp).Row), colP)
'Boucle en "remontant"
Do
boolP = False
'Je split mes cellules Px et Qx
'Dans mes tableaux oTableP et oTableQ, j'aurais des valeurs -ex : {PC02 ; SRSW ; P442 ; P60G ; PR5E ; PR2E}
If InStr(oRng, "/") Then
oTableP = Split(oRng.Offset(0, 0), "/")
boolP = True
'Je met dans ma cellule Px la première valeur de ma oTableP
oRng.Offset(0, 0) = oTableP(LBound(oTableP))
'Je boucle de la deuxième valeur de ma oTableP à la dernière valeur (la première valeur ayant déjà été insérée préalablement)
For i = LBound(oTableP) + 1 To UBound(oTableP)
'J'insère une ligne en dessous
oRng.Offset(i, 0).EntireRow.Insert
'J'y ajoute la valeur de oTableP(i)
oRng.Offset(i, 0) = oTableP(i)
'et je recopie l'intégralité de la ligne
Range(.Cells(oRng.Offset(i, 0).Row, 1), .Cells(oRng.Offset(i, 0).Row, colP - 1)).Value = _
Range(.Cells(oRng.Offset(i, 0).Row - 1, 1), .Cells(oRng.Offset(i, 0).Row - 1, colP - 1)).Value
Next i
End If
If InStr(oRng.Offset(0, 1), "/") Then
oTableQ = Split(oRng.Offset(0, 1), "/")
'Et dans ma Qx, je supprime la valeur
oRng.Offset(0, 1) = ""
If boolP Then
oRng.Offset(i, 0).EntireRow.Insert
oRng.Offset(i, 1) = oTableQ(LBound(oTableQ))
Range(.Cells(oRng.Offset(i, 0).Row, 1), .Cells(oRng.Offset(i, 0).Row, colP - 1)).Value = _
Range(.Cells(oRng.Offset(i, 0).Row - 1, 1), .Cells(oRng.Offset(i, 0).Row - 1, colP - 1)).Value
Else
i = 0
oRng.Offset(0, 1) = oTableQ(LBound(oTableQ))
End If
'Je fais à peu près pareil pour ma oTableQ
'je boucle simplement sur l'ensemble du tableau
For j = LBound(oTableQ) + 1 To UBound(oTableQ)
oRng.Offset(i + j, 0).EntireRow.Insert
oRng.Offset(i + j, 1) = oTableQ(j)
Range(.Cells(oRng.Offset(i + j, 0).Row, 1), .Cells(oRng.Offset(i + j, 0).Row, colP - 1)).Value = _
Range(.Cells(oRng.Offset(i + j, 0).Row - 1, 1), .Cells(oRng.Offset(i + j, 0).Row - 1, colP - 1)).Value
Next j
End If
'Et enfin je décale ma Range à la ligne du dessus pour remonter tranquillement jusqu'à la condition de fin (ici = 3)
Set oRng = oRng.Offset(-1, 0)
Loop Until oRng.Row < oLign
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub |
Partager