0

I have data in excel in following format

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

I have to convert the above data into something like this:

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

Is there any function in excel that can do that. I could find only the row to columns function that didn't help me as it will just transpose the data and not create multiple entries like above.

What could be the best way to do this even through VBA.

4

2 回答 2

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

原来的:

原始数据

结果:

在此处输入图像描述

于 2013-07-01T15:23:13.933 回答
1

我想知道是否可以在没有 VBA 的情况下进行制作,似乎确实如此。但是有一些假设,特别是你正在变换的区域是矩形的。

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

在此处输入图像描述

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

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

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

然后把它拖下来。

于 2013-07-01T14:47:58.157 回答