I have multiple lines of data that refer to x number of females and x number of males per line.
I need to break them out into individual records (1 row for each). Is there an excel formula that would look at the first row and based on the number in column D, return that number of rows and the data in a, b, c is same, however, for columns e, f, g, h, replace that many records with a 1 all in another sheet?
See screen image. There are 9 rows with a 1 in level 1, 17 with a 1 in level 2, etc. I have tried basic formulas combos that work but are time-consuming and there are 1342 rows that need to be broken into individual records. enter image description here
Here is the sheet after the unpivot you suggested David. Table after unpivot
CODE FOR 5 LEVELS with code for slow issues Sub CreateIndividualRecordsLevel5() Dim i As Long Dim a As Long Dim b As Long Dim c As Long Dim d As Long Dim e As Long Dim n As Long Dim instancesInRow As Long Dim level1 As Long Dim level2 As Long Dim level3 As Long Dim level4 As Long Dim level5 As Long Dim wb As Workbook Dim ws As Worksheet
Application.ScreenUpdating = False
Application.Calculation = xlCalculationAutomatic
Application.DisplayStatusBar = False
Application.EnableEvents = False
Set wb = ThisWorkbook
Set ws = wb.Worksheets("WHITE")
n = 0
' Outer loop 'goes' through your original table and for each cell in
' column D checks how many instances there are.
For i = 2 To 523
instancesInRow = ws.Cells(i, 4).Value
level1 = ws.Cells(i, 5).Value
level2 = ws.Cells(i, 6).Value
level3 = ws.Cells(i, 7).Value
level4 = ws.Cells(i, 8).Value
level5 = ws.Cells(i, 9).Value
' Inner loop 'knows' how many times it needs to loop-copy the same
' row into the new table/worksheet.
For a = 1 To level1
n = n + 1
ws.Range("J" & n & ":M" & n).Value = ws.Range("A" & i & ":I" & i).Value
ws.Range("N" & n).Value = 1
ws.Range("O" & n).Value = 0
ws.Range("P" & n).Value = 0
ws.Range("Q" & n).Value = 0
ws.Range("R" & n).Value = 0
Next a
For b = 1 To level2
n = n + 1
ws.Range("J" & n & ":M" & n).Value = ws.Range("A" & i & ":I" & i).Value
ws.Range("N" & n).Value = 0
ws.Range("O" & n).Value = 1
ws.Range("P" & n).Value = 0
ws.Range("Q" & n).Value = 0
ws.Range("R" & n).Value = 0
Next b
For c = 1 To level3
n = n + 1
ws.Range("J" & n & ":M" & n).Value = ws.Range("A" & i & ":I" & i).Value
ws.Range("N" & n).Value = 0
ws.Range("O" & n).Value = 0
ws.Range("P" & n).Value = 1
ws.Range("Q" & n).Value = 0
ws.Range("R" & n).Value = 0
Next c
For d = 1 To level4
n = n + 1
ws.Range("J" & n & ":M" & n).Value = ws.Range("A" & i & ":I" & i).Value
ws.Range("N" & n).Value = 0
ws.Range("O" & n).Value = 0
ws.Range("P" & n).Value = 0
ws.Range("Q" & n).Value = 1
ws.Range("R" & n).Value = 0
Next d
For e = 1 To level5
n = n + 1
ws.Range("J" & n & ":M" & n).Value = ws.Range("A" & i & ":I" & i).Value
ws.Range("N" & n).Value = 0
ws.Range("O" & n).Value = 0
ws.Range("P" & n).Value = 0
ws.Range("Q" & n).Value = 0
ws.Range("R" & n).Value = 1
Next e
' Counter n counts how many rows you have copied so far,
'in order to know in which row to copy to the new range
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayStatusBar = True
Application.EnableEvents = True
End Sub