在列中重新排列行数据

时间:2013-07-01 14:03:00

标签: excel vba excel-vba rows

我在excel中有以下格式的数据

Resource     2/2/2013   2/3/2013  2/4/2013
  Name1         9          9         9  
  Name2         9          9         9  
  Name3         9          9         9  

我必须将上述数据转换成以下内容:

Resource  Date        Hours
Name1     2/2/2013      9
Name1     2/3/2013      9
Name1     2/4/2013      9
Name2     2/2/2013      9
Name2     2/3/2013      9
Name2     2/4/2013      9
Name3     2/2/2013      9
Name3     2/3/2013      9
Name3     2/4/2013      9

excel中是否有任何功能可以做到这一点。我只能找到没有帮助我的row to columns函数,因为它只会transpose数据,而不是像上面那样创建多个条目。

即使通过VBA,这也是最好的方法。

2 个答案:

答案 0 :(得分:1)

我想知道是否可以在没有VBA的情况下制作它似乎是。 但有一些假设,特别是你转化的区域是矩形的。

然后你可以使用QUOTIENT一个MOD函数(可以将“辅助列C-E”合并在一起,但为了更清晰的解释,我展示了它们。)

enter image description here

在A9中,我有一个列数的值(它可以通过另一个函数得到) - 只是有点通用。

然后我以这种方式使用INDIRECT()函数:

  • 单元格G9:=INDIRECT("R"&2+D9&"C1";FALSE)
  • 单元格H9:=INDIRECT("R1C"&2+E9;FALSE)
  • cell I9:=INDIRECT("R"&2+D9&"C"&2+E9;FALSE)

然后将其拖下来。

答案 1 :(得分:1)

这是一个VBA解决方案:

Sub Example()
    Dim Resources() As String
    Dim rng As Range
    Dim row As Long
    Dim col As Long
    Dim x As Long

    ReDim Resources(1 To (ActiveSheet.UsedRange.Rows.Count - 1) * (ActiveSheet.UsedRange.Columns.Count - 1), 1 To 3)

    'Change this to the source sheet
    Sheets("Sheet1").Select

    'Read data into an array
    For row = 2 To ActiveSheet.UsedRange.Rows.Count
        For col = 2 To ActiveSheet.UsedRange.Columns.Count
            x = x + 1
            Resources(x, 1) = Cells(row, 1).Value    ' Get name
            Resources(x, 2) = Cells(1, col).Value    ' Get date
            Resources(x, 3) = Cells(row, col).Value  ' Get value
        Next
    Next

    'Change this to the destination sheet
    Sheets("Sheet2").Select

    'Write data to sheet
    Range(Cells(1, 1), Cells(UBound(Resources), UBound(Resources, 2))).Value = Resources

    'Insert column headers
    Rows(1).Insert
    Range("A1:C1").Value = Array("Resource", "Date", "Value")

    'Set strings to values
    Set rng = Range(Cells(1, 3), Cells(ActiveSheet.UsedRange.Rows.Count, 3))
    rng.Value = rng.Value
End Sub

原件:

Original Data

结果:

enter image description here

相关问题