如何解决比较两个工作表A 和 B:从工作表 A 复制的数据在工作表 B
在分解我的原始问题时,我遇到了这个障碍:当我运行比较代码时,有些会从第一张纸 (A) 复制到第二张纸 (B)。
我将汇总表中的 A 行与辅导表中的 B 行进行比较,并将差异(新数据)复制到 (B)。问题是当我运行代码时,A 行摘要中的一些值在辅导 (B) 中重复,而一些数据根本没有复制。
黄色中的名称应该是添加的名称,但是当我运行代码时,一些数据被复制了,导致表 B 中的数据重复。
这是汇总表,新数据以黄色突出显示,这应该是唯一复制到辅导表(B)的数据
。这是显示旧数据(绿色)、新数据(黄色)和重复数据(红色圈出)的辅导表:
这是我苦苦挣扎了几天的代码:
Private Sub Find_Match_Summary()
On Error Resume Next
Dim ws1 As Worksheet,ws2 As Worksheet
Dim i As Integer,j As Integer,a As Integer,b As Integer
Set ws1 = ActiveWorkbook.Sheets("Summary") ' Column A relates to a = row 4 and i = 4
a = ws1.Cells(ws1.Rows.Count,1).End(xlUp).Row ' Last filled cell in Column A of Summary
i = 4 ' Beginning row of compare for Summary
Set ws2 = ActiveWorkbook.Sheets("Tutoring Attendance") ' Column B relates to b = row 5 and j =5
b = ws2.Cells(ws1.Rows.Count,2).End(xlUp).Row ' Last filled cell in Column B of Tutoring
j = 5 ' Beginning row of compare for Tutoring
For i = 4 To a
ws1.Activate
ws1.Range(i,1).Select ' Select first cell to compare
If Trim(ws1.Cells(i,1).Value2) = Trim(ws2.Cells(j,2).Value2) Then
MsgBox "Cells are True for = " & ws2.Cells(j,2).Value2
Else
ws1.Range("A" & i,"B" & i).copy ' copy the two cells of data
ws2.Activate ' Tutoring Sheet
b = ws2.Cells(ws1.Rows.Count,2).End(xlUp).Row ' Last filled cell in Column B of Tutoring
ws2.Cells(b + 1,2).Select ' First empty cell in Column B of Tutoring
ws2.Range("B50").End(xlUp).Offset(1,0).PasteSpecial xlPasteValues ' Paste VALUES only to new rows
MsgBox "Verify " & ws2.Cells(j,2).Value2 & " was copied over" ' Used to verify correct Value was copied over
End If
Next i
Application.CutcopyMode = False
ws1.Activate ' Tutoring Attendance sheet activated
MsgBox "Find_Match_Summary - Done!"
End Sub
感谢大家的帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。