Ce code doit être ce que vous cherchez, la méthode ExpandData(String, String, String)
prend la colonne de départ pour l'ensemble des données (dans ce cas, « A ») pour le premier paramètre, la colonne de fin pour l'ensemble de données copier en tant que deuxième paramètre (dans ce cas "D") et enfin la colonne avec l'ensemble des données séparées par des virgules ("E" ici).
Vous devriez probablement l'étendre pour qu'il prenne aussi une ligne de départ ou en fasse simplement une formule addin où il faut une plage et une colonne.
Espérons que cela aide.
Sub ExpandDat()
ExpandData "A", "D", "E"
End Sub
Sub ExpandData(start_range As String, end_range As String, comma_column As String)
Const FirstRow = 1
Dim LastRow As Long
LastRow = Range(start_range & CStr(Rows.Count)).End(xlUp).Row
' Get the values from the worksheet '
Dim SourceRange As Range
Set SourceRange = Range(start_range & CStr(FirstRow) & ":" & end_range & CStr(LastRow))
' Get the comma seperated values as a different set of values '
Dim CommaRange As Range
Set CommaRange = Range(comma_column & CStr(FirstRow) & ":" & comma_column & CStr(LastRow))
' Get the values from the actual values '
Dim Vals() As Variant
Vals = SourceRange.Value
' We need to know the upper and lower bounds of the second dimension in the Vals Array '
Dim lower As Integer
Dim upper As Integer
lower = LBound(Vals, 2)
upper = UBound(Vals, 2)
' Get the comma seperated values '
Dim Commas() As Variant
Commas = CommaRange.Value
' Loop through the rows in the array and split each comma-delimited list of items and put each on its own row '
Dim ArrIdx As Long
Dim RowCount As Long
For ArrIdx = LBound(Commas, 1) To UBound(Commas, 1)
Dim CurrList As String
CurrList = Replace(Commas(ArrIdx, 1), " ", "")
' Split the Comma set into an array '
Dim ListItems() As String
ListItems = Split(CurrList, ",")
' For each value in the Comma Seperated values write the output '
Dim ListIdx As Integer
For ListIdx = LBound(ListItems) To UBound(ListItems)
' Loop through the values in our source range and output them '
For Idx = lower To upper
Range(start_range & CStr(FirstRow + RowCount)).Offset(0, Idx - 1).Value = Vals(ArrIdx, Idx)
Next Idx
Range(comma_column & CStr(FirstRow + RowCount)).Value = ListItems(ListIdx)
RowCount = RowCount + 1
Next ListIdx
Next ArrIdx
End Sub
Quel est le code que vous utilisez actuellement? –