简体   繁体   中英

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

This should transfer and transpose the values in each A:E to column G.

Sub AE_2G()
    Dim r As Long, s As Variant
    For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        s = Cells(r, 1).Resize(1, 5)
        Cells((r - 1) * 5 + 1, 7).Resize(5, 1) = Application.Transpose(s)
    Next r
End Sub

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM