2016-04-28 159 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