Sub MyMacro()
Dim lr As Long, lc As Long
Dim r As Long, c As Long
Dim dr As Long
Application.ScreenUpdating = False
' Set initial data row at row 19
dr = 19
' Get number of row and columns
lc = Range("B1").Value
lr = Range("B2").Value
' Loop through all columns starting in column E
For c = 5 To (lc + 4)
' Loop through all rows starting at row 9
For r = 9 To (lr + 8)
' Populate rows in grid
Cells(dr, "B").Value = Cells(r, "B").Value
Cells(dr, "C").Value = Cells(r, "C").Value
Cells(dr, "D").Value = Cells(r, c).Value
Cells(dr, "E").Value = Cells(2, c).Value
Cells(dr, "F").Value = Cells(3, c).Value
Cells(dr, "G").Value = Cells(4, c).Value
Cells(dr, "H").Value = Cells(5, c).Value
' Increment data row counter
dr = dr + 1
Next r
Next c
Application.ScreenUpdating = True
End Sub
thank you x 1000OK, try this:
And here is the proof-of-concept. Everything in black was there initially, and everything in red is what the VBA code created:VBA Code:Sub MyMacro() Dim lr As Long, lc As Long Dim r As Long, c As Long Dim dr As Long Application.ScreenUpdating = False ' Set initial data row at row 19 dr = 19 ' Get number of row and columns lc = Range("B1").Value lr = Range("B2").Value ' Loop through all columns starting in column E For c = 5 To (lc + 4) ' Loop through all rows starting at row 9 For r = 9 To (lr + 8) ' Populate rows in grid Cells(dr, "B").Value = Cells(r, "B").Value Cells(dr, "C").Value = Cells(r, "C").Value Cells(dr, "D").Value = Cells(r, c).Value Cells(dr, "E").Value = Cells(2, c).Value Cells(dr, "F").Value = Cells(3, c).Value Cells(dr, "G").Value = Cells(4, c).Value Cells(dr, "H").Value = Cells(5, c).Value ' Increment data row counter dr = dr + 1 Next r Next c Application.ScreenUpdating = True End Sub
View attachment 110859