VBA code: replicate article information based on year+month combination

etski

New Member
Joined
Jan 7, 2020
Messages
5
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi, I am new to the forum and have just begun to learn VBA. I have a task that I would like to automate but I do not even know where to start. I believe there must be an easy way to solve this but I am not that advanced yet so I guess I am making this more complicated than it actually is.

I would be very thankful for every advice!


CURRENT SITUATION:
I have placed the following scenario on Sheet1:
  • a table with client number, article number, product category and amount. Below an example "input table" in columns C-F (in reality there are more rows)
  • there are 5 different product categories and an article can only belong to one category
  • cell A2 shows the current month in YYYYMM format (assumed now is Feb 2018)
Sheet1:
1578392362264.png



I need to convert this table on Sheet1 to another format. I have placed the required format on Sheet2 and it looks like this:



REQUIRED FORMAT

Sheet2:
1578392694809.png


Columns Client & Amount
1) The information can directly be taken from the input table on Sheet1 (Client, Amount)

Columns Category 1 - Category 5
2) The columns category 1 to category 5 correspond to the column "Product Category" in the input table on Sheet1 --> idea is to place the article code to the category column where it belongs, according to the information that we have in column E on Sheet 1. E.g. article QWERT belongs to Category 2 so article code QWERT will be placed in column F (=Category 2) on Sheet 2.

Columns Business Unit and Key Customer
3) Column Business Unit is always filled with the value "XYZ" and column Key Customer remains always blank

Column Month
4) The column Month needs to be in format YYYYMM. I need to consider which month it currently is and create rows for the current & preceding months of the current year.
  • E.g. if now is February 2018, I need to have a row for January 2018 and February 2018, for each of the article lines that I have in the "input table" on Sheet1. These rows are otherwise identical - the only difference is the year+month combination.
  • The trick is to replicate each article information line on Sheet1 as many times as the current month is. If it were December 2018, I would have twelve lines (one for each month of 2018) for one article information line on Sheet1.


EXAMPLE:
Assumed now is February 2018 (corresponds to value in cell A2 on Sheet1)
Below is how my "output" table on Sheet2 should look like:
1578395029106.png



Does anybody have an idea how I could easily solve this? I find it especially difficult to formulate a macro that replicates the article information based on the year+month combination... and to make it flexible so that it does not matter whether it is 2022 or 2018.

Many thanks in advance!!

Kind regards,
etski
 
Hi Cal, that works except that I need to have the article code in the product category columns instead of the category.. (It is quite stupid but this is a template requirement)

Kind regards, etski
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Sorry, correct this line:

VBA Code:
ShtDest.Cells(ResRw, ColNum).Value = ShtSrc.Range("E" & Rw).Value

to this:

VBA Code:
ShtDest.Cells(ResRw, ColNum).Value = ShtSrc.Range("D" & Rw).Value
 
Upvote 0
Thanks, that works like magic! :) I really appreciate the help!
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top