在 header 上方的数据之前将 Header 行中的每个单元格作为单独的列插入
Insert Each Cell in Header Row as An Individual Column BEFORE the data the header is above
我有一个 excel 工作簿,我需要在其中获取 header 信息,并将其作为列插入。我当前的 set-up 看起来像这样(除了数据向右和向下延伸得更远)
我需要一个宏来格式化数据,如下所示:
Excel 2013 VBA 可以实现这样的功能吗?
编辑
我不是要转置 header 行。我试图在 header 之前插入一个空白列并将值写入新插入的列。
这样就行了
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
我有一个 excel 工作簿,我需要在其中获取 header 信息,并将其作为列插入。我当前的 set-up 看起来像这样(除了数据向右和向下延伸得更远)
我需要一个宏来格式化数据,如下所示:
Excel 2013 VBA 可以实现这样的功能吗?
编辑
我不是要转置 header 行。我试图在 header 之前插入一个空白列并将值写入新插入的列。
这样就行了
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