需要帮助编写代码以将特定数据从一个 sheet 移动到 Excel 中的另一个

Need help writing code to move specific Data from one sheet to another in Excel

基本上我需要将数据从一个 sheet 移动到 Excel 中的另一个。我附上了数据 sheet(我需要从中提取数据)和目的地 sheet(我需要将数据 post 提取到)的照片。我需要编写一个代码来读取 sheet,并为每个 activity(清洁、拖地、擦洗、擦拭等)创建一个新行,并在每个上花费正确的小时数,如何他们完成的单元数、姓名和年份。我附上了一张我手动完成的几行的照片,但如果我可以自动化这个过程,那就容易多了。非常感谢您提供的任何帮助:)

Source and Destination Forms, side by side

所以这不是我做过的最干净的代码,但我相信它会填写我理解的部分,您可以填写其余部分。

由于所有员工都在同一 sheet 上,因此它从 i 向下循环到 sheet 的底部。它检查单元格文本中的 "PP",如果找到它,它将开始检查要传输的 units/hours 行。如果该行上的类型没有错误,它将复制该类型的数据。它为每种类型重复此操作。

如果 sheet 上的单元格文本中没有 "PP",那么它会选择检查 "Employee" 名称,以便它知道要复制到 [=23] 的谁的名称=] 两个用于以下条目。

现在的代码真的很难看,因为我让它不断地指定每个单元格以从中获取值。这是我获取您在第一个 sheet 中列出的所有内容的最快方式,同时让您将来可以轻松更新它。

Sub Report()

    Dim ws1 As Worksheet
    Set ws1 = Sheets(1)

    Dim ws2 As Worksheet
    Set ws2 = Sheets(2)

    Dim i As Long
    Dim row_current As Long
    Dim employee_name_row As Long: employee_name_row = 1

    For i = 1 To ws1.Cells(ws1.Rows.count, "A").End(xlUp).row
        If InStr(ws1.Cells(i, "A").Value2, "PP") > 0 Then

            'getting row to use on sheet 2
            row_current = ws2.Cells(ws2.Rows.count, "A").End(xlUp).Offset(1, 0).row

            If Not IsError(ws1.Cells(i, "D")) Then  'Cleaning

                'period, name
                ws2.Cells(row_current, "A").Value2 = ws1.Cells(i, "A").Value2
                ws2.Cells(row_current, "B").Value2 = ws1.Cells(employee_name_row, "B").Value2

                'task, units, hours
                ws2.Cells(row_current, "C").Value2 = ws1.Cells(3, "B").Value2
                ws2.Cells(row_current, "E").Value2 = ws1.Cells(i, "C").Value2
                ws2.Cells(row_current, "F").Value2 = ws1.Cells(i, "B").Value2

                'year
                ws2.Cells(row_current, "I").Value2 = ws1.Cells(employee_name_row, "L").Value2

                row_current = row_current + 1

            End If
            If Not IsError(ws1.Cells(i, "G")) Then  'Mopping

                'period, name
                ws2.Cells(row_current, "A").Value2 = ws1.Cells(i, "A").Value2
                ws2.Cells(row_current, "B").Value2 = ws1.Cells(employee_name_row, "B").Value2

                'task, units, hours
                ws2.Cells(row_current, "C").Value2 = ws1.Cells(3, "E").Value2
                ws2.Cells(row_current, "E").Value2 = ws1.Cells(i, "F").Value2
                ws2.Cells(row_current, "F").Value2 = ws1.Cells(i, "E").Value2

                'year
                ws2.Cells(row_current, "I").Value2 = ws1.Cells(employee_name_row, "L").Value2

                row_current = row_current + 1

            End If
            If Not IsError(ws1.Cells(i, "J")) Then  'Scrubbing

                'period, name
                ws2.Cells(row_current, "A").Value2 = ws1.Cells(i, "A").Value2
                ws2.Cells(row_current, "B").Value2 = ws1.Cells(employee_name_row, "B").Value2

                'task, units, hours
                ws2.Cells(row_current, "C").Value2 = ws1.Cells(3, "H").Value2
                ws2.Cells(row_current, "E").Value2 = ws1.Cells(i, "I").Value2
                ws2.Cells(row_current, "F").Value2 = ws1.Cells(i, "H").Value2

                'year
                ws2.Cells(row_current, "I").Value2 = ws1.Cells(employee_name_row, "L").Value2

                row_current = row_current + 1

            End If
            If Not IsError(ws1.Cells(i, "M")) Then  'Wiping

                'period, name
                ws2.Cells(row_current, "A").Value2 = ws1.Cells(i, "A").Value2
                ws2.Cells(row_current, "B").Value2 = ws1.Cells(employee_name_row, "B").Value2

                'task, units, hours
                ws2.Cells(row_current, "C").Value2 = ws1.Cells(3, "K").Value2
                ws2.Cells(row_current, "E").Value2 = ws1.Cells(i, "L").Value2
                ws2.Cells(row_current, "F").Value2 = ws1.Cells(i, "K").Value2

                'year
                ws2.Cells(row_current, "I").Value2 = ws1.Cells(employee_name_row, "L").Value2

                row_current = row_current + 1

            End If
            If Len(ws1.Cells(i, "N")) > 0 Then  'Jumping

                'period, name
                ws2.Cells(row_current, "A").Value2 = ws1.Cells(i, "A").Value2
                ws2.Cells(row_current, "B").Value2 = ws1.Cells(employee_name_row, "B").Value2

                'task, hours
                ws2.Cells(row_current, "C").Value2 = ws1.Cells(3, "N").Value2
                ws2.Cells(row_current, "F").Value2 = ws1.Cells(i, "N").Value2

                'year
                ws2.Cells(row_current, "I").Value2 = ws1.Cells(employee_name_row, "L").Value2

                row_current = row_current + 1

            End If
            If Len(ws1.Cells(i, "O")) > 0 Then  'Swimming

                'period, name
                ws2.Cells(row_current, "A").Value2 = ws1.Cells(i, "A").Value2
                ws2.Cells(row_current, "B").Value2 = ws1.Cells(employee_name_row, "B").Value2

                'task, hours
                ws2.Cells(row_current, "C").Value2 = ws1.Cells(3, "O").Value2
                ws2.Cells(row_current, "F").Value2 = ws1.Cells(i, "O").Value2

                'year
                ws2.Cells(row_current, "I").Value2 = ws1.Cells(employee_name_row, "L").Value2

                row_current = row_current + 1

            End If
            If Len(ws1.Cells(i, "P")) > 0 Then  'Other

                'period, name
                ws2.Cells(row_current, "A").Value2 = ws1.Cells(i, "A").Value2
                ws2.Cells(row_current, "B").Value2 = ws1.Cells(employee_name_row, "B").Value2

                'task, hours
                ws2.Cells(row_current, "C").Value2 = ws1.Cells(3, "P").Value2
                ws2.Cells(row_current, "F").Value2 = ws1.Cells(i, "P").Value2

                'year
                ws2.Cells(row_current, "I").Value2 = ws1.Cells(employee_name_row, "L").Value2

                row_current = row_current + 1

            End If
            If Len(ws1.Cells(i, "Q")) > 0 Then  'Computer Probs

                'period, name
                ws2.Cells(row_current, "A").Value2 = ws1.Cells(i, "A").Value2
                ws2.Cells(row_current, "B").Value2 = ws1.Cells(employee_name_row, "B").Value2

                'task, hours
                ws2.Cells(row_current, "C").Value2 = ws1.Cells(3, "Q").Value2
                ws2.Cells(row_current, "F").Value2 = ws1.Cells(i, "Q").Value2

                'year
                ws2.Cells(row_current, "I").Value2 = ws1.Cells(employee_name_row, "L").Value2

                row_current = row_current + 1

            End If

        ElseIf InStr(ws1.Cells(i, "A").Value2, "Name") > 0 Then
            employee_name_row = i
        End If

    Next i
End Sub