As seen below, I have 3 sheets, viz: Sheet "Shops-Fruits Data", Sheet ("Months"), and Sheet ("Output").
I am trying to copy the data from Sheet "Shops-Fruits Data" based on months from Sheet ("Months") to ("Output") structure. I have written a code. However, with this code I can only iterate through first row. I don't understand, how do I continue to the next row till the last row. Second, I also can't copy the Shops and fruits name to the Sheet ("Output").
I have manually copied the desired result in Sheet Output table as seen below, there you can see what I want to achieve. It would be great if anyone can lead me! Thank you.
Sheets("Shops-Fruits Data")
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 2021 | 2021 | 2021 | 2021 | 2021 | 2021 | 2021 | 2021 | 2021 | 2021 | 2021 | 2021 | |||
2 | Shop | Fruits | Quantity | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
3 | Walmart | Apple | Quantity | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 10 | 11 | 12 |
4 | Walmart | Orange | Quantity | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
5 | D-Mart | Apple | Quantity | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 |
Sheets ("Months")
A | |
---|---|
1 | JAN |
2 | FEB |
3 | MAR |
4 | APR |
5 | MAY |
6 | JUN |
7 | JUL |
8 | AUG |
9 | SEP |
10 | OCT |
11 | NOV |
12 | DEC |
Sheets ("Output")
Shop | Fruits | Year | Month | Quantity |
---|---|---|---|---|
Walmart | Apple | 2021 | JAN | 10 |
Walmart | Apple | 2021 | FEB | 20 |
Walmart | Apple | 2021 | MAR | 30 |
Walmart | Apple | 2021 | APR | 40 |
Walmart | Apple | 2021 | MAY | 50 |
Walmart | Apple | 2021 | JUN | 60 |
Walmart | Apple | 2021 | JUL | 70 |
Walmart | Apple | 2021 | AUG | 80 |
Walmart | Apple | 2021 | SEP | 90 |
Walmart | Apple | 2021 | OCT | 10 |
Walmart | Apple | 2021 | NOV | 11 |
Walmart | Apple | 2021 | DEC | 12 |
Walmart | Orange | 2021 | JAN | 12 |
Walmart | Orange | 2021 | FEB | 13 |
Walmart | Orange | 2021 | MAR | 14 |
Walmart | Orange | 2021 | APR | 15 |
Walmart | Orange | 2021 | MAY | 16 |
Walmart | Orange | 2021 | JUN | 17 |
Walmart | Orange | 2021 | JUL | 18 |
Walmart | Orange | 2021 | AUG | 19 |
Walmart | Orange | 2021 | SEP | 20 |
Walmart | Orange | 2021 | OCT | 21 |
Walmart | Orange | 2021 | NOV | 22 |
Walmart | Orange | 2021 | DEC | 23 |
D-Mart | Apple | 2021 | JAN | 36 |
D-Mart | Apple | 2021 | FEB | 38 |
D-Mart | Apple | 2021 | MAR | 40 |
D-Mart | Apple | 2021 | APR | 42 |
D-Mart | Apple | 2021 | MAY | 44 |
D-Mart | Apple | 2021 | JUN | 46 |
D-Mart | Apple | 2021 | JUL | 48 |
D-Mart | Apple | 2021 | AUG | 50 |
D-Mart | Apple | 2021 | SEP | 52 |
D-Mart | Apple | 2021 | OCT | 54 |
D-Mart | Apple | 2021 | NOV | 56 |
D-Mart | Apple | 2021 | DEC | 58 |
The code I am trying:
Sub test()
Dim c As Range, d As Range, ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, LastRow As Long
Dim Mon As String
Set ws1 = Sheets("Shops-Fruits Data")
Set ws2 = Sheets("Months")
Set ws3 = Sheets("Output")
LastRow = ws2.Cells(Rows.Count, 1).End(xlUp).Row
i = 2
For Each c In ws2.Range("A1:A" & LastRow)
Mon = c.Value
With ws1.Range("D2:O2")
Set d = .Find(Mon, , LookIn:=xlValues)
With ws3.Range("D:D")
'Copy Months
.Cells(i, 1) = c.Value
'Copy Year
.Cells(i, 0) = d.Offset(-1, 0).Value
'Copy Quantity
.Cells(i, 2) = d.Offset(1, 0).Value
'Copy Fruit Name till December.
.Cells(2, -1) = d.Offset(1, -1).Value 'But it fails!
'Copy Shop Name till December.
.Cells(2, -2) = d.Offset(1, -2).Value 'But it fails!
i = i + 1
'How do I continue to next row now?
End With
End With
Next c
End Sub