将包含100列的Excel表格展开到行中

时间:2014-07-31 17:17:18

标签: excel ms-office

我的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列我想以这种方式取消。

2 个答案:

答案 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)

  1. 制作新表
  2. 将第一列(已经正确)复制到新工作表中
  3. 删除原始工作表中的第一列。
  4. 突出显示第1行并在其上方插入新行
  5. 填写标题
  6. 在A列前插入新列
  7. 粘贴您所制作的新表格的第一列。