excel - 比较两个工作表(A 和 B):从工作表 A 复制的数据会在工作表 B 中创建重复项
问题描述
在分解我原来的问题时,我遇到了这个障碍:当我运行比较代码时,有些被从第一张表(A)复制到第二张表(B)。
我将摘要表中的 A 行与辅导表中的 B 行进行比较,并将差异(新数据)复制到(B)。问题是当我运行代码时,A Summary 行中的一些值在 Tutoring (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
请看看你能做些什么来帮助我找到我的代码错误。
谢谢大家的帮助!
解决方案
推荐阅读
- c++ - 为什么我收到错误,包括 MSVS2019 上的 ENet 库
- python - Databricks 错误:ModuleNotFoundError:没有名为“sqlalchemy”的模块
- javascript - 根据复选框值切换 div - 带动画
- datetime - Nifi将UTC转换为unix时间
- c# - 为什么对 C# 数组使用“新”语法?
- sql - ClickHouse 列在处理 dateDiff 时来自不同的表
- javascript - 反应 redux 多个 http 请求调度
- c# - 如何在 C# 中使用“NEX 530 NFC RFID”阅读器?
- python - 使用 Python / Pandas 读取多个 JSON 文件并附加到数据集
- winforms - VS2019 自动添加HideSelection