我有一个excel工作簿,我需要获取标题信息,并将其作为列插入。我当前的设置看起来像这样(除了数据向右和向下延伸得更远)
使用Excel 2013 VBA可以实现这样吗?
修改 我不是试图转置标题行。我试图在标题之前插入一个空白列,并将值写入新插入的列。
答案 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