在所需的时间内将数据从行转置到列

时间:2016-02-06 07:06:56

标签: excel vba excel-vba

我需要在另一列的行中转置一行文本,如下所示: (需要在相关的第2行[此处为A2至E3]时间将A1复制到E1文本)

    A     B     C       D       E       F
1   Ab1     Ab2     Ab3     Ab4     Ab5     Total     
2   3   2   1   3   1   10
3                       
4   Ab1                 
5   Ab1                 
6   Ab1                 
7   Ab2                 
8   Ab2                 
9   Ab3                 
10  Ab4                 
11  Ab4                 
12  Ab4                 
13  Ab5     

2 个答案:

答案 0 :(得分:0)

这将遍历Row1

Sub Move_It()
    Dim sh As Worksheet
    Dim col As Long
    Dim rng As Range
    Dim c As Range, x, y

    Set sh = Sheets("Sheet1")    'actual name of worksheet

    With sh
        col = .Cells(1, .Columns.Count).End(xlToLeft).Column
        Set rng = .Range(.Cells(1, 1), .Cells(1, col))

        For Each c In rng.Cells    'loop through row 1
            x = c.Offset(1)

            For y = 1 To x

                .Cells(.Rows.Count, "A").End(xlUp).Offset(1) = c

            Next y

        Next c

    End With

End Sub

答案 1 :(得分:0)

  • 选择来源范围 - >鼠标右键单击 - > "复印&#34 ;;
  • 选择目标范围的左上角单元格 - >鼠标右键单击 - > "粘贴特殊" - > "粘贴特殊"对话 - >检查" Transpose"复选框 - >单击"确定"

您可以将其录制为宏。