将标题行中的每个单元格作为单个列插入标题高于

时间:2017-09-11 12:50:53

标签: excel vba excel-vba excel-2013

我有一个excel工作簿,我需要获取标题信息,并将其作为列插入。我当前的设置看起来像这样(除了数据向右和向下延伸得更远) Current Set Up

我需要一个宏来格式化数据,如下所示: enter image description here

使用Excel 2013 VBA可以实现这样吗?

修改 我不是试图转置标题行。我试图在标题之前插入一个空白列,并将值写入新插入的列。

1 个答案:

答案 0 :(得分:1)

这样做

Sub Macro2()
Dim c As Integer, i As Integer
Dim myheader As String

c = range("b2").End(xlToRight).Column
For i = 1 To c
    Columns(2 * i + 1).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    myheader = Cells(1, 2 * i).Value
    Cells(2, 2 * i + 1).Value = myheader
    Cells(2, 2 * i + 1).Select
    Selection.AutoFill destination:=range(Selection, Selection.Offset(0, -1).End(xlDown).Offset(0, 1))


Next i

End Sub

修改

Sub Macro2()
Dim c As Integer, i As Integer
Dim myheader As String

c = range("b2").End(xlToRight).Column
For i = 1 To c
    Columns(2 * i).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    myheader = Cells(1, 2 * i + 1).Value
    Cells(2, 2 * i).Value = myheader
    Cells(2, 2 * i).Select
    Selection.AutoFill destination:=range(Selection, Selection.Offset(0, -1).End(xlDown).Offset(0, 1))
Next i

End Sub