我的Excel表格中有一个表格如下:
Code _01001 _01002
----------------------
1 88 371
2 88 371
3 88 371
我想将其转换为以下格式:
Code Column Value
----------------------
1 _01001 88
1 _01002 371
2 _01001 88
2 _01002 371
3 _01001 88
3 _01002 371
是否有开箱即用的功能?如果没有,最好的方法是什么。目前我显示了2列,但实际文件中有大约100列我想以这种方式取消。
答案 0 :(得分:0)
最近我为我的朋友写了一个功能,它完全符合你的需要。打开VBA编辑器,添加新模块,然后将其粘贴到那里,然后运行。
Option Explicit
Sub Macro1()
Dim i As Long
Dim j As Long
Dim mrow As Long
Dim mcol As Long
i = 0
mcol = 4
mrow = Cells(Rows.Count, 1).End(xlUp).Row
Cells(1, mcol - 1).EntireColumn.Insert shift:=xlToRight
Cells(1, 2).Copy
Cells(1, 3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Cells(mrow * i + 1, 3), Cells(mrow + mrow * i, 3)).FillDown
i = i + 1
While (Cells(1, mcol).Value2 <> "" And i < 200)
'' copy data
Range(Cells(1, mcol), Cells(mrow, mcol)).Copy
Cells(mrow * i + 1, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'' copy dates
Range(Cells(1, 1), Cells(mrow, 1)).Copy
Cells(mrow * i + 1, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'' fill down country
Cells(mrow * i + 1, 2).Copy
Cells(mrow * i + 1, 3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(Cells(mrow * i + 1, 3), Cells(mrow + mrow * i, 3)).FillDown
'' delete copied data
Range(Cells(1, mcol), Cells(mrow, mcol)).Clear
'' increase counter
i = i + 1
mcol = mcol + 1
Wend
For j = i To 1 Step -1
Cells(j * mrow + 1, 1).Select
Selection.EntireRow.Delete (xlUp)
Next j
End Sub
答案 1 :(得分:-1)