Uploaded July 2019 | Updated September 2026, 2 weeks ago
How to unstack data automatically with VBA. Here's the complete VBA code:
Option Explicit
Sub unstackData()
Dim lastrow As Long
lastrow = Sheets("Sheet1").Range("A" & Rows.count).End(xlUp).Row
'Range("D2") = "abcA2"
'Range("E2") = "abcA3"
'Range("F2") = "abcA4"
'Range("D3") = "abcA5"
'Range("E3") = "abcA6"
'Range("F3") = "abcA7"
prefillData
lastrow = lastrow / 3
Range("D2:F3").AutoFill Destination:=Range("D2:F" & (lastrow + 1)), Type:=xlFillDefault
Range("D2:F" & (lastrow + 1)).Replace what:="abc", replacement:="="
Range("G1").Select
End Sub
Option Explicit
Sub prefillData()
Dim i As Long, j As Long, k As Long
i = 2
j = 4
k = 2
Do While Cells(i, 1) not-equal-to ""
Cells(i, j) = "abcA" & k
j = j + 1
k = k + 1
If k = 5 Then
j = 4
i = 3
Cells(i, j) = "abcA" & k
k = k + 1
j = j + 1
ElseIf k greater than 7 Then Exit Sub
End If
Loop
End Sub
Visit our website to download a sample file for practice: exceltrainingvideos.com/unstack-data-automatically
How to unstack data automatically with VBA. Here's the complete VBA code:
Option Explicit
Sub unstackData()
Dim lastrow As Long
lastrow = Sheets("Sheet1").Range("A" & Rows.count).End(xlUp).Row
'Range("D2") = "abcA2"
'Range("E2") = "abcA3"
'Range("F2") = "abcA4"
'Range("D3") = "abcA5"
'Range("E3") = "abcA6"
'Range("F3") = "abcA7"
prefillData
lastrow = lastrow / 3
Range("D2:F3").AutoFill Destination:=Range("D2:F" & (lastrow + 1)), Type:=xlFillDefault
Range("D2:F" & (lastrow + 1)).Replace what:="abc", replacement:="="
Range("G1").Select
End Sub
Option Explicit
Sub prefillData()
Dim i As Long, j As Long, k As Long
i = 2
j = 4
k = 2
Do While Cells(i, 1) not-equal-to ""
Cells(i, j) = "abcA" & k
j = j + 1
k = k + 1
If k = 5 Then
j = 4
i = 3
Cells(i, j) = "abcA" & k
k = k + 1
j = j + 1
ElseIf k greater than 7 Then Exit Sub
End If
Loop
End Sub
Visit our website to download a sample file for practice: exceltrainingvideos.com/unstack-data-automatically










