2016-04-28 155 views
-3

我有三个列在Excel:Microsoft Excel中 - 合并和整合问题

Column A: Category 
Column B: Value 
Column C: Value 

我想巩固列表,例如,如果我有一个像下面行:

A1:Car  | B1:Ford | C1:Toyota 
A2:Scooter | B2:Honda | C2:(blank) 
A3:Bike | B3:Yamaha | C3:Ducati 

给我一个结果:

A1:Car  | B1:Ford 
A2:Car  | B2:Toyota 
A3:Scooter | B3:Honda 
A4:Bike | B4:Yamaha 
A5:Bike | B5:Ducati 

Image explaining the issue

+0

Marco建议的宏工作!非常感谢所有 – Adi

回答

0

下面的代码可以做到这一点,因为这一切都发生在activesheet上。

Sub consolidate() 

Dim lastRow As Integer 
Dim lastCol As Integer 
Dim counterRow As Integer 
Dim counterCol As Integer 
Dim counterDestinationRow As Integer 
Dim destination As Object 

lastRow = Cells(Rows.Count, 1).End(xlUp).Row 

Set destination = Cells(lastRow + 2, 1) 
'or to another sheet: Set destination = Sheets("Sheet2").Cells(1, 1) 

For counterRow = 1 To lastRow 

    lastCol = Cells(counterRow, Columns.Count).End(xlToLeft).Column 

    For counterCol = 2 To lastCol 

     destination.Offset(counterDestinationRow, 0) = Cells(counterRow, 1) 
     destination.Offset(counterDestinationRow, 1) = Cells(counterRow, counterCol) 

     counterDestinationRow = counterDestinationRow + 1 

    Next counterCol 

Next counterRow 

End Sub 
+0

嗨马可。它部分工作。有电子邮件地址可以与您分享实际的电子表格吗? – Adi

+0

当然,使用[email protected] –

+0

谢谢Marco。刚发送。非常感谢 – Adi

0

无需编程,只需复制粘贴。

步骤0:保存为新SHEET :)

  1. 插入在C之前的柱(这是新的列C)
  2. 从柱A类到列C(复制为文本)复制值
  3. 选择在列C和d的所有值,直接最后的值A以下复制粘贴和B.
  4. 删除列c和d
  5. 排序上柱A然后B,具有空值的列
  6. 除去空行
+0

谢谢Sascha。 谢谢1月 问题是,这不是一次性的事情。 这是一份月度报告。 使用索引vlookup公式我从输入文件中获得了'Ford'和'Toyota'等'Car'类别的值。 现在我需要使用公式将输出转换为所需的格式(在单独的表格中),以便输出文件可以输入到数据透视图和图形中 – Adi

+0

有什么可以从源获得的?数据来自哪里? –

+0

嗨,Jan.谢谢。来源是来自定期报告,并包含相当多的专栏。我需要在现有的矩阵中查找并为输入列的连接找到对应的值。问题是查找结果是多个,所以我带他们使用索引公式(如福特和丰田在我的问题声明) – Adi