基本上,我需要在Excel中将数据从一张纸移动到另一张纸。我已经附上了数据表(需要从中提取数据)和目标表(需要将数据发布到)的照片。我需要编写代码来阅读工作表,并为每个活动(清洁,拖布,擦洗,擦拭等)创建新行,并指定正确的工作时间,完成的单位数,名称,和一年。我已经附上了我手动完成的几行照片,但是如果我可以使过程自动化,那会容易得多。非常感谢您可以提供的任何帮助:)
答案 0 :(得分:0)
所以这不是我做过的最干净的代码,但是我相信它会填满我理解的部分,您可以填写其余部分。
由于员工全都位于同一张纸上,因此从i一直循环到纸的底部。它将在单元格文本中检查“ PP”,如果找到它,它将开始检查该行中要传输的单位/小时。如果该行上的类型没有错误,它将复制该类型的数据。每种类型重复此操作。
如果在第一页的单元格文本中没有“ PP”,则它将另选“雇员”的姓名,以便它知道要为以下条目复制到第二页的姓名。
现在代码真的很丑,因为我不断指定每个单元格来获取值。对于我来说,这是最快的方法,可让您在第一张工作表中列出的所有内容,同时使您将来可以轻松更新。
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