2016-09-29 63 views
0

目前我有两个报告,它们的格式不同,希望使用宏或公式将其中一个报告转换为与另一个报告相同的格式将两份报告合并在一起。列到行的度量(以及从列到行的日期)

由于两个报告提供不同的指标,但基于相同的名称日期(如链段)。我想知道是否有办法做到这一点,而不是每个日期范围手动透视表并手动转换所有内容。

下面是一个示例,我想将第一个包含指标(食物A,B和C)的报告按行排列,而将日期(16-Sep-201625-Sep-2016)排列在列中以相反方式排列下表的图片。

示例图片:

enter image description here

希望从任何人很快听到!

谢谢:)

+0

只是想知道如果第一个表总是有相同数量的名字,准确地每食物相同的名称(即4个A,4个B和4个C)?那么你可以用一个相当简单的公式来做到这一点。 –

+0

您好@TomSharpe对于实际的报告,在“名称”,“日期”和“食物”有一个巨大的变体,所以我一直遇到像运行时错误溢出等问题。哈哈 –

回答

1

这是远不够理想,只是一个快速解决方案,但它的工作。它将图片中的上表转换为另一张。您的原始表必须在第一层上,并在运行宏之前,您需要一个空的第二片:

Sub TableChanger() 

    Dim i As Integer, j As Integer, k As Integer 
    Dim n As Integer 

    Sheets(2).Cells.ClearContents 
    Sheets(2).Cells(1, 1).Value = "Date" 
    Sheets(2).Cells(1, 2).Value = "Name" 

    i = 2 
    n = 2 
    While Sheets(1).Cells(i, 1).Value <> "" 

     j = 3 
     While Sheets(1).Cells(i, 1).Value <> Sheets(2).Cells(1, j).Value And Sheets(2).Cells(1, j).Value <> "" 
      j = j + 1 
     Wend 
     If Sheets(2).Cells(1, j).Value = "" Then 
      Sheets(2).Cells(1, j).Value = Sheets(1).Cells(i, 1).Value 
     End If 

     i = i + 1 
    Wend 

    i = 3 
    While Sheets(1).Cells(1, i).Value <> "" 
     j = 2 
     While Sheets(1).Cells(j, 1).Value <> "" 

      k = 3 
      While Sheets(2).Cells(1, k).Value <> Sheets(1).Cells(j, 1).Value 
       k = k + 1 
      Wend 

      n = 2 
      While (Sheets(1).Cells(1, i).Value <> Sheets(2).Cells(n, 1).Value Or Sheets(1).Cells(j, 2).Value <> Sheets(2).Cells(n, 2).Value) And Sheets(2).Cells(n, 1) <> "" 
       n = n + 1 
      Wend 

      Sheets(2).Cells(j, 1).Value = Sheets(1).Cells(1, k).Value 
      If Sheets(2).Cells(j, 1).Value <> "" Then Sheets(2).Cells(j, 2).Value = Sheets(1).Cells(j, 2).Value 
      Sheets(2).Cells(n, k).Value = Sheets(1).Cells(j, i).Value 

      j = j + 1 
     Wend 
     i = i + 1 
    Wend 
End Sub 
+0

嗨特雷托姆,只是好奇,如果它是动态的,例如,如果除了提到的3个以外还有更多的名称,还有更多的食物和日期,是否有可能? –

+0

哦,是的,如果有运行时溢出错误,我该如何解决这个问题?哈哈谢谢你! –

+0

@Ka Chee Ho:我只在少数情况下测试过它。增加了额外的列和行,对我来说它工作。 溢出错误:好的,我没有实现任何错误处理。你可以在VBA – tretom