I have an Excel with the following structure which I want to convert to another structure:

The actual file is much more complex than this – but I created this schematic to describe the essence of the problem. The file currently has around 5K rows, but it is expected to contain around 50K-100K entries. So, the solution should have a good performance.
What I have in mind is
- Copy columns from Customer Name to Unit Price and Year 1 Quantity & Year 1 TotalCost and paste it in the destination range and add a column year number and populate it with 1
- Copy columns from Customer Name to Unit Price and Year 2 Quantity & Year 2 TotalCost
and populate year number column with 2
The questions I have are these:
- Will this solution perform well?
- Are there other solutions that avoids copying and pasting multiple times?
- Is there a way to update the source range itself without pasting the data in a new destination range?
Homework I have done:
I did a google search and tried to read as many article as I could. I also read the following threads in Stackoverflow, but none of them has the answer that I am looking for
Converting Excel rows to columns (smarter than transpose)
I think you should consider transferring this application to Access or other database. The rest of this answer assumes that this is not possible at this time.
The approach you are considering has a slow loop:
I would :
If you need example code, I am happy to provide some. I could code for your example sheet but some characteristics of the real worksheets would give you more useful code for little extra effort on my part.
Part 2
You say “The actual file is much more complex than this – but I created this schematic to describe the essence of the problem.”
I have assumed:
I created worksheet Input and created the 20 data rows. I duplicated data rows 3 to 22 down to create 5,000 data rows. I assume this is a fair representation of your data:
The output from the macro is in worksheet Output:
This is what I believe you seek. I have rearranged the data as specified. I have copied the format of the header rows, the column widths and the number formats across. If you have formulae in the input they will be values in the output.
For 5,000 rows the macro takes about .1 seconds to copy the data and about .05 seconds to apply the formatting.
In the code I have included comments to say what I am doing and why I am doing it but there are not many comments explaining the VBA statements. For example the first statement is
Option Explicit. It is easy to look this up in VB Help or you can search the internet for “Excel VBA Option Explicit”. Come back with questions if necessary.Hope this helps.