Search code examples
excelexcel-2007transposeflattenvba

VBA excel macro to process 5000 rows and 5 columns


I am using Excel 2007 and have 5000 statements in Range A1:A5000 and four alternatives for each statement in range B1:E5000. I want to convert it to another slyle where all data comes in range G1:25000 where all four alternatives will come under every Statement. I recorded a macro but I dont know how to loop it and doing manually is impossible. Need some help here... thanks.

Example:

Statement1 - AlternativeA 1 -  AlternativeA 2 -  AlternativeA 3 -  AlternativeA 4
Statement2 - AlternativeB 1 -  AlternativeB 2 -  AlternativeB 3 -  AlternativeB 4
Statement3 - AlternativeC 1 -  AlternativeC 2 -  AlternativeC 3 -  AlternativeC 4
Statement4 - AlternativeD 1 -  AlternativeD 2 -  AlternativeD 3 -  AlternativeD 4

Output:

Statement1 
AlternativeA 1
AlternativeA 2
AlternativeA 3
AlternativeA 4
Statement2
AlternativeB 1
AlternativeB 2
AlternativeB 3
AlternativeB 4
Statement3
AlternativeC 1
AlternativeC 2
AlternativeC 3
AlternativeC 4
Statement4
AlternativeD 1
AlternativeD 2
AlternativeD 3
AlternativeD 4

Solution

  • You don't really need VBA for this. Merely enter the formula

    G1: =INDEX($A:$E,INT((ROWS($1:1)-1)/5)+1,MOD(ROWS($1:1)-1,5)+1)

    and fill down to G25000. Then copy and paste-special values