0

我有一个包含 200 多个具有相同结构的 Excel 电子表格的工作簿。在这些工作表上,主题的值始终位于单元格 C2 中,日期的值始终位于 C7 中,但对于Root_causeSolutions,它们从不同的行开始。

https://imgur.com/aIxHJtH

我需要在主工作表上复制这些信息并附加它:

在此处输入图像描述

也许使用查找功能找到“Root_cause”这个词是个好主意,然后选择右侧的一列并向下拖动以复制所有相关行?

代码:

Sub Protocol()
Dim wsheet As Worksheet

With ThisWorkbook.Sheets("Main")
    For Each wsheet In ThisWorkbook.Sheets
        If wsheet.Name <> "Main" Then

                Set Date = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
                Set Theme = .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0)
                Set Root_cause = .Cells(.Rows.Count, "C").End(xlUp).Offset(1, 0)
                Set Solutions = .Cells(.Rows.Count, "D").End(xlUp).Offset(1, 0)

                Date.Value = wsheet.Range("C7").Value
                Theme.Value = wsheet.Range("C2").Value
                #Then I need to use FIND function on each sheet, come to word 'Root_cause', choose all rows for Root_cause and Solutions, copy them and append on sheet "Main"

                End If

        Debug.Print wsheet.Name
    Next wsheet

End With

End Sub
4

1 回答 1

0

这是使用查找功能并限制在“根本原因”标签正下方的单元格的解决方案。

Sub SelectActualUsedRange()

Dim FirstCell As Range, LastCell As Range

Set LastCell = Cells(Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column)

Set FirstCell = Cells(Cells.Find(What:="root_cause", After:=LastCell, SearchOrder:=xlRows, _
SearchDirection:=xlNext, LookIn:=xlValues).Row, _
Cells.Find(What:="root_cause", After:=LastCell, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, LookIn:=xlValues).Column)

Range(FirstCell.Offset(1, 0), LastCell.Offset(0, -5)).Copy

End Sub

请注意,我创建了两张表,其中一张“根本原因”从第 13 行开始,另一张从第 25 行开始。其余的只是选择主表并将“根本原因”值粘贴到该表上。这是两张单独的纸张的图像。

在此处输入图像描述

编辑:请注意,我只选择中间的两列,从找到“根本原因”的位置一直到最后一个非空单元格。

于 2019-02-18T15:05:47.607 回答