Microsoft Excel - 合并和合并问题

时间:2016-04-28 13:04:54

标签: excel merge

我在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

2 个答案:

答案 0 :(得分:0)

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

第0步:保存为新页:)

  1. 在C之前插入一列(这是新的C列)
  2. 将值从列A类复制到列C(复制为文本)
  3. 选择C和D列中的所有值,直接在最后一个值A和B下复制粘贴。
  4. 删除列c和d
  5. 对A列进行排序,然后对B进行排序,删除空值为空的列

答案 1 :(得分:0)

以下代码可以解决这个问题,因为这一切都发生在活动表上。

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