-2

i have list of products with sales numbers for each month. i want to create new table which duplicate the values based on month and to write the month in additional column.

This is the table

Main Table

and this is the result i want

Result Table

Thank you

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
M S
  • 1
  • 1

1 Answers1

1

Please, try the next code:

Sub testTransposePerMonth()
 Dim sh As Worksheet, sh1 As Worksheet, lastR As Long, arr, arrF
 Dim i As Long, k As Long, j As Long, n As Long, maxF As Long

 Set sh = ActiveSheet
 Set sh1 = sh.Next 'use here the sheet you need
 
 lastR = sh.Range("A" & rows.count).End(xlUp).row 'last row of the existing sheet
 arr = sh.Range("A1:O" & lastR).Value             'put the range in an array
 maxF = WorksheetFunction.Sum(sh.Range("D2:O" & lastR)) + 1 'calculate the arrF rows No

 ReDim arrF(1 To maxF, 1 To 4)                    'Redim the final array
 arrF(1, 1) = "Product Number": arrF(1, 2) = "City"
 arrF(1, 3) = "Region": arrF(1, 4) = "Month"      'Put headers in the array
 k = 2
 For i = 2 To UBound(arr)       'iterate between all the array elements
    For j = 4 To UBound(arr, 2) 'iterate between the array cols, starting from the fourth
        If arr(i, j) <> "" Then 'if it is a value on the row
            For n = 1 To CLng(arr(i, j)) 'add so many rows as the number means
                arrF(k, 1) = arr(i, 1): arrF(k, 2) = arr(i, 2)
                arrF(k, 3) = arr(i, 3): arrF(k, 4) = arr(1, j): k = k + 1 'iterate k
            Next
        End If
    Next j
 Next i
 'drop the processed array at once:
 sh1.Range("A1").Resize(UBound(arrF), 4).Value = arrF
End Sub
FaneDuru
  • 38,298
  • 4
  • 19
  • 27
  • Thank You very much, this answer solved it for me. much appreciated – M S Jan 14 '21 at 11:30
  • @M S: Glad I could help! But take care, in case of another question, if you do not prove your own involvement in solving the problem, I will be the first voting for closing it... – FaneDuru Jan 14 '21 at 11:45
  • i tried power query, unpivot and other things. a friend of mine suggested to look for VBA code. i usually look for answers only, literally i searched this site for days for similar case. unfortunately couldn't find what am looking for. sorry for not following your standers, this is my first question. will do my best next time. thank you – M S Jan 14 '21 at 12:20
  • @M S: It was not a matter of a 'similar case'. You should try something based on your logic. I used a faster version, using two arrays, but the problem could be solved using ranges. too... I mean, you must prove somehow that you tried something on your own. This is the community spirit. We do not write code for people needing it. We only help them to understand their mistakes and learn... I make an exception in order to help, but mobilizing you to try on your own, too. Anyhow, there are many cases of rows transposed on columns. Only me I answered many similar questions. Not identic, of course. – FaneDuru Jan 14 '21 at 12:24
  • 1
    next time i will share all the steps i did and any work i tried before asking :) thanks – M S Jan 14 '21 at 12:27
  • @M S: Not all of them, but something eloquent will be welcomed. If I wouldn't answer fast enough, your question should be closed, I am afraid... Our community has some rules regarding the way you ask a question. – FaneDuru Jan 14 '21 at 12:32