如何在excel中将多行转换为单行?

时间:2016-07-13 04:03:07

标签: excel excel-vba vba

我的数据如下表所示:

pre

我的数据如上表所示。这个表有很多MMID M000071,M000073,....等。筛选期可能因资产而异。每个MMID可能有几个日期。

我想要输出如下:

after

表中有20,000个。所以我想找到一个简单的方法来完成这项工作。

2 个答案:

答案 0 :(得分:1)

@Jeeped给我一个使用VBA的建议。

因此,通过几个小时的Excel VBA学习,我已经解决了这个问题,并获得了我想要的答案。

代码如下:

Option Explicit

Public Sub sort()
    Dim j As Long
    Dim cursor As Long
    j = 2
    cursor = 2

    For j = 2 To 20150
X:      If Worksheets("Sheet2").Cells(cursor, "A").Value = "" Then
            Worksheets("Sheet2").Cells(cursor, "A").Value = Worksheets("Sheet1").Cells(j, "A").Value
            Worksheets("Sheet2").Cells(cursor, "H").Value = Worksheets("Sheet1").Cells(j, "D").Value
            Select Case Worksheets("Sheet1").Cells(j, "B")
                Case "ACR"
                    Worksheets("Sheet2").Cells(cursor, "B") = Worksheets("Sheet1").Cells(j, "C")
                Case "Microalbumin"
                    Worksheets("Sheet2").Cells(cursor, "C") = Worksheets("Sheet1").Cells(j, "C")
                Case "eGFR"
                    Worksheets("Sheet2").Cells(cursor, "D") = Worksheets("Sheet1").Cells(j, "C")
                Case "LDL_HDL_TotChol"
                    Worksheets("Sheet2").Cells(cursor, "E") = Worksheets("Sheet1").Cells(j, "C")
                Case "HbA1C"
                    Worksheets("Sheet2").Cells(cursor, "F") = Worksheets("Sheet1").Cells(j, "C")
                Case "UricAcid"
                    Worksheets("Sheet2").Cells(cursor, "G") = Worksheets("Sheet1").Cells(j, "C")
            End Select
        Else
            If Worksheets("Sheet1").Cells(j, "A").Value = Worksheets("Sheet2").Cells(cursor, "A").Value Then
                If Worksheets("Sheet1").Cells(j, "D").Value = Worksheets("Sheet2").Cells(cursor, "H").Value Then
                    Select Case Worksheets("Sheet1").Cells(j, "B")
                        Case "ACR"
                            Worksheets("Sheet2").Cells(cursor, "B") = Worksheets("Sheet1").Cells(j, "C")
                        Case "Microalbumin"
                            Worksheets("Sheet2").Cells(cursor, "C") = Worksheets("Sheet1").Cells(j, "C")
                        Case "eGFR"
                            Worksheets("Sheet2").Cells(cursor, "D") = Worksheets("Sheet1").Cells(j, "C")
                        Case "LDL_HDL_TotChol"
                            Worksheets("Sheet2").Cells(cursor, "E") = Worksheets("Sheet1").Cells(j, "C")
                        Case "HbA1C"
                            Worksheets("Sheet2").Cells(cursor, "F") = Worksheets("Sheet1").Cells(j, "C")
                        Case "UricAcid"
                            Worksheets("Sheet2").Cells(cursor, "G") = Worksheets("Sheet1").Cells(j, "C")
                    End Select
                Else
                    cursor = cursor + 1
                    GoTo X
                End If
            Else
                cursor = cursor + 1
                GoTo X
            End If
        End If
    Next

End Sub

我希望这可以帮助像我这样的其他人。

注意:在运行此程序之前,应将sheet2的单元格MESDATE设置为Text。否则,它会将sheet1的单元格MSEDATE更改为Date类型。

答案 1 :(得分:0)

在Excel中使用文本到列选项。

1)将数据复制到Excel

2)选择数据

3)在数据功能区下 - >选择文本到列

4)选择分隔选项并单击下一步

5)选择Delimter As Space(选中复选框)并单击下一步

6)选择目的地并按完成

Step1

Step2

Step3

Step4