Sub Test2()
Dim i As Long, j As Long, K As Long, St As String, Lr As Long, Lc As Long
Dim Cn As Long, Sn As Long, m As Long, n As Long, p As Long, q As Long, r As Long
Dim m2 As Long, q2 As Long, r2 As Long, n2 As Long
Dim X1 As Long, X2 As Long, X3 As Long, X4 As Long, X5 As Long
With Sheets("Sheet1")
X1 = Application.WorksheetFunction.Match("Url image", .Range("A1:E1"), 0)
X2 = Application.WorksheetFunction.Match("Price and Stock", .Range("A1:E1"), 0)
X3 = Application.WorksheetFunction.Match("Color", .Range("A1:E1"), 0)
X4 = Application.WorksheetFunction.Match("Size", .Range("A1:E1"), 0)
Lr = .Cells(Rows.Count, X2).End(xlUp).Row
Sheets("Sheet2").Range("A1:E1").Value = Array("Color1", "Size1", "Price1", "Stock1", "Url Image1")
For i = 2 To Lr
Cn = Len(.Cells(i, X3)) - Len(Application.WorksheetFunction.Substitute(.Cells(i, X3), ",", "")) + 1
Sn = Len(.Cells(i, X4)) - Len(Application.WorksheetFunction.Substitute(.Cells(i, X4), ",", "")) + 1
For j = 1 To Cn
If j = 1 Then
m = 1
q = 1
r = 1
Else
m = InStr(m + 1, .Cells(i, X3).Value, ",")
q = InStr(q + 1, .Cells(i, X2).Value, ",")
r = InStr(r + 1, .Cells(i, X1).Value, ",")
End If
m2 = InStr(m + 1, .Cells(i, X3).Value, ",")
If m2 = 0 Then m2 = Len(.Cells(i, X3).Value) + 1
q2 = InStr(q + 1, .Cells(i, X2).Value, ",")
If q2 = 0 Then q2 = Len(.Cells(i, X2).Value) + 1
r2 = InStr(r + 1, .Cells(i, X1).Value, ",")
If r2 = 0 Then r2 = Len(.Cells(i, X1).Value) + 1
For K = 1 To Sn
If K = 1 Then
n = 1
Else
n = InStr(n + 1, .Cells(i, X4).Value, ",")
End If
n2 = InStr(n + 1, .Cells(i, X4).Value, ",") + 1
If n2 = 1 Then n2 = Len(.Cells(i, X4).Value) + 1
If Len(.Cells(i, X3).Value) > 0 Then
St = St & "," & Mid(.Cells(i, X3).Value, m, m2 - m)
Else
St = St & "," & "##"
End If
If Len(.Cells(i, X4).Value) > 0 Then
St = St & "," & Mid(.Cells(i, X4).Value, n, n2 - n)
Else
St = St & "," & "##"
End If
If K > 1 Then
q = InStr(q + 1, .Cells(i, X2).Value, ",")
q2 = InStr(q + 1, .Cells(i, X2).Value, ",")
If q2 = 0 Then q2 = Len(.Cells(i, X2).Value) + 1
End If
St = St & "," & Replace(Mid(.Cells(i, X2).Value, q, q2 - q), ":", ",")
If Len(.Cells(i, X1).Value) > 0 Then
St = St & "," & Mid(.Cells(i, X1).Value, r, r2 - r)
Else
St = St & "," & "##"
End If
Next K
Next j
St = Replace(Replace(Replace(Replace(St, " ", ""), ",,", ","), ",,", ","), "##", "")
St = Right(St, Len(St) - 1)
Debug.Print St
Sheets("Sheet2").Range("A" & i).Resize(, Cn * Sn * 5).Value = Split(St, ",")
St = ""
Next i
End With
Lc = Sheets("Sheet2").Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
Sheets("Sheet2").Range("A1:E1").AutoFill Destination:=Range(Sheets("Sheet2").Cells(1, 1), Sheets("Sheet2").Cells(1, Lc)), Type:=xlFillDefault
St = Split(Cells(1, Lc).Address, "$")(1)
'Sheets("Sheet2").Columns("A:" & St).AutoFit
End Sub