Hi,
click the button on the first sheet and check result on 2nd sheet: test_multiple_cells.xlsm
contains:
Option Explicit
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
RestrucData
Application.ScreenUpdating = True
End Sub
Sub RestrucData()
Dim dicData As Object: Set dicData = CreateObject("Scripting.Dictionary")
Dim dicChild As Object
Dim aData, sValue
Dim lngMaxRows As Long, i As Long, j As Integer
With ThisWorkbook.Worksheets("Sheet1") 'adapt sheetname
lngMaxRows = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To lngMaxRows
sValue = .Cells(i, 2).Value
If InStr(sValue, vbLf) > 0 Then 'return
aData = Split(sValue, vbLf)
ElseIf InStr(sValue, ",") > 0 Then 'comma
aData = Split(sValue, ",")
ElseIf InStr(sValue, " ") > 0 Then 'space
aData = Split(sValue, " ")
End If
Set dicChild = CreateObject("Scripting.Dictionary")
If IsArrayAllocated(aData) Then
For j = LBound(aData) To UBound(aData)
dicChild(j) = Array(.Cells(i, 1).Value, Trim(aData(j)), .Cells(i, 3).Value)
Next j
Erase aData
Else
dicChild(0) = Array(.Cells(i, 1).Value, .Cells(i, 2).Value, .Cells(i, 3).Value)
End If
dicData.Add i, dicChild
Next i
End With
With ThisWorkbook.Worksheets("Sheet2") 'adapt sheetname
lngMaxRows = .Cells(.Rows.Count, 1).End(xlUp).Row
For Each aData In dicData.Keys
For Each sValue In dicData(aData).Keys
.Range(.Cells(lngMaxRows, 1), .Cells(lngMaxRows, 3)).Value = dicData(aData)(sValue)
lngMaxRows = lngMaxRows + 1
Next sValue
Next aData
.Activate
End With
End Sub
Function IsArrayAllocated(arr) As Boolean
On Error GoTo ErrHandler
Dim tmpVal
tmpVal = LBound(arr)
IsArrayAllocated = True
Exit Function
ErrHandler:
IsArrayAllocated = False
End Function
Bookmarks