[英]How to consolidate data of Specific columns from multiple sheet to one sheet using excel macro when the required columns are not in same order
我在具有10個項目表的Excel工作表中對不同項目有缺陷,例如工作表A,工作表B,工作表C,工作表D,工作表E,工作表F等
每張紙都有不同的列, 缺陷編號,缺陷摘要,嚴重性,優先級,報告者,分配給者,狀態,日期等
我想從所有工作表到所有工作表的列( 所有工作表中的列都不相同 )中(缺陷合並)
缺陷編號,缺陷摘要,嚴重性,優先級和狀態
當單擊一個按鈕時,我需要從A,B和C項目表中看到所有上述字段,而不是從所有項目中看到。
希望有人能幫上忙。
注意 :我可以手動復制粘貼它,但是要求是到目前為止,我們只需要在工作簿中創建一個名稱為“缺陷合並”的excel工作表,這就是所有:(對宏而言有點新
系統詳細信息:Windows 7和Microsoft提供2010。
正如我對您的問題的解釋一樣。 如果您的要求有所不同,這應該為您提供一個起點。
將以下代碼附加到您的按鈕上...
Option Explicit
Sub consolidate()
Dim myInSht As Worksheet
Dim myOutSht As Worksheet
Dim aRow As Range
Dim aCol As Range
Dim myInCol As Range
Dim myOutCol As Range
Dim cell As Range
Dim iLoop As Long, jLoop As Long
jLoop = 2
' loop through the worksheets
For Each myInSht In ActiveWorkbook.Worksheets
' pick only the worksheets of interest
If myInSht.Name = "PrjA" Or myInSht.Name = "PrjB" Or myInSht.Name = "PrjC" Then
' find the columns of interest in the worksheet
For Each aCol In myInSht.UsedRange.Columns
Set myOutCol = Nothing
If aCol.Cells(1, 1).Value = "Defect id" Then Set myOutCol = Sheets("Consolidated").Range("A:A")
If aCol.Cells(1, 1).Value = "Defect summary" Then Set myOutCol = Sheets("Consolidated").Range("B:B")
If aCol.Cells(1, 1).Value = "severity" Then Set myOutCol = Sheets("Consolidated").Range("C:C")
If aCol.Cells(1, 1).Value = "priority" Then Set myOutCol = Sheets("Consolidated").Range("D:D")
If aCol.Cells(1, 1).Value = "status" Then Set myOutCol = Sheets("Consolidated").Range("E:E")
If Not myOutCol Is Nothing Then
' don't move the top line, it contains the headers - no data
Set myInCol = aCol
Set myInCol = myInCol.Offset(1, 0).Resize(myInCol.Rows.Count - 1, myInCol.Columns.Count)
' transfer data from the project tab to the consolidated tab
iLoop = jLoop
For Each aRow In myInCol.Rows
myOutCol.Cells(iLoop, 1).Value = aRow.Cells(1, 1).Value
iLoop = iLoop + 1
Next aRow
End If
Next aCol
End If
If iLoop > jLoop Then jLoop = iLoop
Next myInSht
End Sub
項目A的內容...
項目B的內容...
項目C的內容...
在運行合並例程后...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.