我有两张包含员工记录的工作表。 Sheet1中包含事件日期,CardNo,员工姓名,部门编号,员工号,进入和退出时间,累计工作时间,状态,ConcatinatedColumn和备注使用VBA将一行从一张纸复制到另一张
Sheet2中包含ConcatinatedColumn,活动日期(通过从Sheet2的VLOOKUP复制),员工号,姓名,备注。
如果sheet2的备注栏中的数据是“Sick Off”,那么应将该行插入Sheet1而不影响以前的记录。
我已经为它编写了代码,但它不起作用。
如果有人能帮助我,我会很感激!
感谢提前!
我的代码:
Sub SickOff()
Dim objWorksheet As Sheet2
Dim rngBurnDown As Range
Dim rngCell As Range
Dim strPasteToSheet As String
'Used for the new worksheet we are pasting into
Dim objNewSheet As Sheet1
Dim rngNextAvailbleRow As Range
'Define the worksheet with our data
Set objWorksheet = ThisWorkbook.Worksheets("Sheet2")
'Dynamically define the range to the last cell.
'This doesn't include and error handling e.g. null cells
'If we are not starting in A1, then change as appropriate
Set rngBurnDown = objWorksheet.Range("G2:G" & objWorksheet.Cells(Rows.Count, "G").End(xlUp).Row)
'Now loop through all the cells in the range
For Each rngCell In rngBurnDown.Cells
objWorksheet.Select
If rngCell.Value = "Sick Off" Then
'select the entire row
rngCell.EntireRow.Select
'copy the selection
Selection.Copy
'Now identify and select the new sheet to paste into
Set objNewSheet = ThisWorkbook.Worksheets("Sheet1" & rngCell.Value)
objNewSheet.Select
'Looking at your initial question, I believe you are trying to find the next available row
Set rngNextAvailbleRow = objNewSheet.Range("A1:A" & objNewSheet.Cells(Rows.Count, "A").End(xlUp).Row)
Range("A" & rngNextAvailbleRow.Rows.Count + 1).Select
ActiveSheet.Paste
End If
Next rngCell
objWorksheet.Select
objWorksheet.Cells(1, 1).Select
'Can do some basic error handing here
'kill all objects
If IsObject(objWorksheet) Then Set objWorksheet = Nothing
If IsObject(rngBurnDown) Then Set rngBurnDown = Nothing
If IsObject(rngCell) Then Set rngCell = Nothing
If IsObject(objNewSheet) Then Set objNewSheet = Nothing
If IsObject(rngNextAvailbleRow) Then Set rngNextAvailbleRow = Nothing
End Sub
'如果Sheet 2中的备注栏中的数据是“病关“,那么该行应插入sheet1而不影响previo我们记录。“插入行不是一个问题,但是两张表都有不同位置的标题。这不是问题吗? –
是的,它会..但现在我无法找出复制粘贴行也知道如何根据他们特定的标题获取行?如果你能帮助我,那会很好..请! –
我可以看到工作簿的样本吗?如果是,那么你可以在www.wikisend.com上传相同的内容并在此分享链接? –