【问题标题】:Microsoft Excel - Merging and consolidating issueMicrosoft Excel - 合并和合并问题
【发布时间】:2016-08-23 06:31:06
【问题描述】:

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

【问题讨论】:

  • Marco 建议的宏成功了!非常感谢大家

标签: excel merge


【解决方案1】:

下面的代码可以解决问题,假设这一切都发生在活动表上。

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

【讨论】:

  • 嗨,马可。它部分工作。是否有可以与您分享实际电子表格的电子邮件地址?
  • 当然,使用 marcotemp@yopmail.com
【解决方案2】:

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

第 0 步:另存为新工作表 :)

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

【讨论】:

  • 谢谢萨沙。谢谢 Jan。问题是它不是一次性的。是月报。使用索引 vlookup 公式,我从输入文件中获得了针对“汽车”类别的“福特”和“丰田”等值。现在我需要使用公式将输出转换为所需的格式(在单独的表格中),以便输出文件可以输入到数据透视表和图表中
  • 从源头有什么收获吗?数据从何而来?
  • 嗨,一月,谢谢。来源来自定期报告,包含不少专栏。我需要在现有矩阵中查找并为输入列的连接找到相应的值。问题是查找结果是多个,所以我使用索引公式(比如我的问题陈述中的福特和丰田)将它们带过来
猜你喜欢
  • 2021-04-25
  • 2019-09-10
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 2015-10-07
  • 2012-01-18
  • 2013-08-22
  • 2011-06-28
相关资源
最近更新 更多