简体   繁体   English

Excel VBA代码,用于将数据从行转置为列

[英]Excel VBA code to transpose data from rows to columns

I have data in column A with 50000 rows of data. 我在A列中有50000行数据。 I need to transpose every 6 rows of data to 6 columns. 我需要将每6行数据转换为6列。 For example data from A1:A6 must be transposed to B1:G1. 例如,来自A1:A6的数据必须转换为B1:G1。 Again data from A7:A14 must be transposed to B2:G2. 同样来自A7:A14的数据必须转换为B2:G2。 I appreciate if anyone can provide VBA code for this. 我很感激,如果有人能为此提供VBA代码。

Data I have in column A is as shown below: 我在A栏中的数据如下所示:

Col A
1
2
3
4
5
6
7
8
9
10
11
12

The transpose data must be as shown below in col B to col G: 转置数据必须如下面的col B到col G所示:

Columns  B   C   D   E   F   G
         1   2   3   4   5   6
         7   8   9   10  11  12

Try this: 尝试这个:

Sub TransposeRows()
    Dim rng As Range
    Dim i As Long

    Set rng = Range("A1")
    While rng.Value <> ""
        i = i + 1
        rng.Resize(6).Copy
        Range("B" & i).PasteSpecial Transpose:=True
        Set rng = rng.Offset(6)            
    Wend
    Application.CutCopyMode = False
End Sub

Got this from here . 这里得到这个。

You do not need a macro for this. 你不需要一个宏。 In B1 enter: B1输入:

=OFFSET($A$1,COLUMNS($A:A)-1+(ROWS($1:1)-1)*6,0)

Then copy both across and down: 然后向上和向下复制:

在此输入图像描述

additional variant from my side: 我方的其他变种:

Sub TransposeRows2()
    Dim i&, z&, x&
    i = Cells(Rows.Count, "A").End(xlUp).Row
    z = 1: x = 1
    While z <= i
        Range("B" & x).Resize(, 6) = _
            WorksheetFunction.Transpose(Range("A" & z).Resize(6))
        z = z + 6: x = x + 1
    Wend
End Sub

tested: 测试:

在此输入图像描述

在葡萄牙语Excel中,“加里学生”的公式为4列而不是6列,变为:

=DESLOCAMENTO($A$1;COLS($A:A)-1+(LINS($1:1)-1)*4;0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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