简体   繁体   中英

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

I have defects for different project in an excel sheet with 10 project sheets eg sheet A, Sheet B Sheet C ,Sheet D ,sheet E,Sheet F etc

Each sheet has different columns, Defect id, defect summary, severity, priority, reported by, assigned to, status, date etc

I want following columns (which is NOT IN SAME ORDER in all sheets ) from all sheets to one sheet(defectconsolidation)

Defect id,Defect summary,severity,priority and status

When clicked a button , I need to see all above fields from JUST A,B and C project sheet and not from all project.

hope some one will help.

Note : I can copy paste it manually but the requirement is we need to have a macro so far just created a excel sheet with name "defect consolidation" in my workbook thats all :( am bit new to macro

system details : windows 7 and microsoft offer 2010.

This does as I interpreted your question. This should provide you a starting point if your requirements are a little different.

Attach the following code to your button ...

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

I used the following to test ... The consolidated tab before consolidation ... 在此处输入图片说明

Project A contents ...

在此处输入图片说明

Project B contents ...

在此处输入图片说明

Project C contents ...

在此处输入图片说明

And after running the consolidation routine ...

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM