Unstack Data Automatically

How to unstack data automatically using VBA. Many a times we receive data from certain databases or accounting software arranged one below the other. Such data with multiple items arranged in a stack is not easy to analyze. We need to unstack the data for a better and quicker analysis. The video below describes how to unstack such data manually and automatically:

Here’s the complete VBA code:

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) <> “”

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 > 7 Then Exit Sub

End If

Loop
End Sub

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

The recorded macro:

Option Explicit

Sub Macro1()

‘ Macro1 Macro


Range(“D2”).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = “=RC[-3]”
Range(“E2”).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = “=R[1]C[-4]”
Range(“F2”).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = “=R[2]C[-5]”
Range(“D2”).Select
ActiveCell.FormulaR1C1 = “abcA2”
Range(“E2”).Select
ActiveCell.FormulaR1C1 = “abcA3”
Range(“F2”).Select
ActiveCell.FormulaR1C1 = “abcA4”
Range(“D3”).Select
ActiveCell.FormulaR1C1 = “abcA5”
Range(“E3”).Select
ActiveCell.FormulaR1C1 = “abcA6”
Range(“F3”).Select
ActiveCell.FormulaR1C1 = “abcA7”
Range(“D2:F3”).Select
ActiveWindow.SmallScroll Down:=-12
Application.CellDragAndDrop = True
Selection.AutoFill Destination:=Range(“D2:F43”), Type:=xlFillDefault
Range(“D2:F43″).Select
Selection.Replace what:=”abc”, replacement:=”=”, lookat:=xlPart, _
searchorder:=xlByRows, MatchCase:=False, searchformat:=False, _
ReplaceFormat:=False, formulaversion:=xlReplaceFormula2
Selection.NumberFormat = “#,##0;-#,##0;”
Range(“F16”).Select
ActiveWindow.SmallScroll Down:=-12
End Sub

Here’s a sample file for download and practice:

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.