【问题标题】: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当所需列的顺序不同时,如何使用excel宏将特定列的数据从多个工作表合并到一张工作表
【发布时间】:2016-04-06 07:34:23
【问题描述】:

我在一个包含 10 个项目表的 Excel 表中存在不同项目的缺陷,例如表 A、表 B、表 C、表 D、表 E、表 F 等

每个工作表都有不同的列,缺陷 ID、缺陷摘要、严重性、优先级、报告者、分配给、状态、日期

我想要以下列(所有工作表中的顺序不同)从所有工作表到一张工作表(缺陷合并)

缺陷 ID、缺陷摘要、严重性、优先级和状态

单击按钮时,我需要查看仅 A、B 和 C 项目表中的所有上述字段,而不是所有项目中的所有字段。

希望有人能提供帮助。

注意:我可以手动复制粘贴,但要求是我们需要有一个宏 到目前为止,刚刚在我的工作簿中创建了一个名为“缺陷合并”的 Excel 表 就是这样:(我对宏有点新

系统详细信息:Windows 7 和 microsoft 提供 2010。

【问题讨论】:

    标签: vb.net vba excel excel-2010


    【解决方案1】:

    这就像我解释你的问题一样。如果您的要求稍有不同,这应该为您提供一个起点。

    将以下代码附加到您的按钮...

    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 内容 ...

    在运行合并例程之后...

    【讨论】:

    • 你能告诉我如何标记它吗,对不起,我在这里很新......提前谢谢。
    • @OldUgle 我可以给出这样的多个条件吗?或者我有相同的值字段但名称不同?如果 aCol.Cells(1, 1).Value = "Defect ID" 或 "Key" 然后设置 myOutCol = Sheets("Consolidated").Range("A:A")
    • 你试过了吗? OR、AND 和 NOT 在 IF THEN 逻辑中均有效。
    • 是的,试过了,它抛出了错误。在我的 excel 工作簿中,我在多个不同标题的工作表中存在缺陷。 ,如果我可以连接或它应该在找到它时选择值
    • 抱歉,我现在看到语法错误。 If (aCol.Cells(1,1).Value = "Defect ID") Or (aCol.Cells(1,1).Value = "Key") Then Set myOutCol = Sheets("Consolidated").Range("A:A")
    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多