如何将行值与列互换

时间:2018-04-06 07:17:23

标签: excel vba excel-vba

我的数据看起来像

empid       time
1            8:00
1            18:00
1            19:00

我希望我的数据看起来像

empid   time1      time2      time3
1        8:00       18:00      19:00

是否可以使用 VB脚本或thourgh pivots 在Excel中执行此操作。

3 个答案:

答案 0 :(得分:0)

下面的代码很简单,必须在代码中注释。我假设您将数据存储在AB列中。

试试这个:

'use this declaration, this helps you avoid mistyped variable names
Option Explicit
Sub transpose()
    'declaration of variables, with Option Explicit, it's necessary
    Dim lastRow As Long, i As Long, table As Variant, colNumber As Long, rowNumber As Long
    'set the values of variables
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    colNumber = 3
    rowNumber = 2
    'read values from range and clear it
    table = Range("A2:B" & lastRow).Value2
    Range("A3:B" & lastRow).Clear
    'loop through values from range and transpose it
    For i = 2 To lastRow - 1
        Do While table(i - 1, 1) = table(i, 1)
            Cells(rowNumber, colNumber).Value = table(i, 2)
            'increment column number with every record within the same empid
            colNumber = colNumber + 1
            i = i + 1
            'if we recahed the end, exit outer for loop
            If i = lastRow Then
                Exit For
            End If
        Loop
        'increment row number if we reached new empid
        rowNumber = rowNumber + 1
        colNumber = 3
        'if came upon new empid, write empid in A column and first value next to it (in B column)
        Cells(rowNumber, 1).Value = table(i, 1)
        Cells(rowNumber, 2).Value = table(i, 2)
    Next

End Sub

答案 1 :(得分:0)

此代码完全符合您的示例

Sub main()
    With Range("A2", Cells(Rows.Count, 1).End(xlUp))
        .Cells(1, 2).Resize(, .Rows.Count).Value = Application.Transpose(.Offset(, 1).Value)
        .Cells(1, 2).Resize(, .Rows.Count).NumberFormat = "h:mm;@"
        .Offset(1).Resize(.Rows.Count - 1, 2).ClearContents

        With .Offset(-1, 1).Resize(1, .Rows.Count)
            .Formula = "=concatenate(""" & .Cells(1, 1).Value & """, Column()-1)"
            .Value = .Value
        End With
    End With
End Sub

答案 2 :(得分:0)

因此,可以从A1读入以下假设连续范围。它将订购您的empID,并且不要求它们最初订购。

Option Explicit

Sub Transpose()

    Dim myArr()

    With ActiveSheet

        myArr() = .Range("A1").CurrentRegion.Value

        Dim i As Long
        Dim sList As Object
        Set sList = CreateObject("System.Collections.Sortedlist")

        For i = 2 To UBound(myArr, 1)               
            Dim currVal As Double
            currVal = myArr(i, 2) * 24

            If Not sList.Contains(myArr(i, 1)) Then  
                sList.Add myArr(i, 1), Format$(Int(CLng(currVal)), "00") & ":" & Format$(Int(CLng(currVal) / 60), "00")       
            Else           
                sList(myArr(i, 1)) = sList(myArr(i, 1)) & ";" & Format$(Int(CLng(currVal)), "00") & ":" & Format$(Int(CLng(currVal) / 60), "00")  
            End If       
        Next i

        Dim columnCount As Long

        For i = 0 To sList.Count - 1                
             With .Cells(i + 1, .Range("A1").CurrentRegion.Columns.Count + 2)           
                .Value = sList.GetKey(i)
                columnCount = UBound(Split(sList.GetByIndex(i), ";")) + 1
                .Offset(, 1).Resize(1, columnCount) = Split(sList.GetByIndex(i), ";")
                .Offset(, 1).Resize(1, columnCount).NumberFormat = "h:mm;@"
            End With 
        Next I           
    End With

End Sub