I'm working with an extract file in Excel. It's basically multiple columns with several row data on each.
A | B | C | D | E | F |
1 | 2 | 3 | 1 | 2 | 3 |
4 | 5 | 5 | 4 | 5 | 5 |
I would like to flatten it into 3 columns, like this :
A | B | C |
1 | 2 | 3 |
4 | 5 | 5 |
D | E | F |
1 | 2 | 3 |
4 | 5 | 5 |
I'd like to do it using VBA but I'm really new to this language, here is what I've done so far :
Sub test()
Dim Key, Dic As Object, cl As Range, Data As Range, i&, n&
Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = vbTextCompare
i = Cells(Rows.Count, "A").End(xlUp).Row
n = 1
Set Data = Range("B2:B" & i & "," & "D2:D" & i & "," & "F2:F" & i & "," & "H2:H" & i)
Dic.Add "|ID", "Date|Thing"
For Each cl In Data
If Cells(cl.Row, "A") <> "" Then
Dic.Add n & "|" & Cells(cl.Row, "A"), cl.Text & "|" & cl.Offset(, 1).Text
n = n + 1
End If
Next cl
n = 1
For Each Key In Dic
Cells(n, "K") = Split(Key, "|")(1)
Cells(n, "L") = Split(Dic(Key), "|")(0)
Cells(n, "M") = Split(Dic(Key), "|")(1)
n = n + 1
Next Key
End Sub
It gives me this result :
A | A | A |
B | B | B |
C | C | C |
1 | 1 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
4 | 4 | 4 |
5 | 5 | 5 |
6 | 6 | 6 |
D | D | D |
E | E | E |
F | F | F |
1 | 1 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
4 | 4 | 4 |
5 | 5 | 5 |
6 | 6 | 6 |
Could you help me please ?