【发布时间】:2022-12-01 00:56:19
【问题描述】:
我正在尝试编写一个宏,该宏将查看 sheet1 上的 A 列,并查看它是否缺少 sheet2 上的 A 列或 sheet3 上的 A 列中的任何值。如果缺少,则将值添加到 sheet1 上 A 列的底部。 sheet2 和 sheet3 上可能存在相同的值,但只需要在 sheet1 上表示一次。
我正在使用下面的代码。
Sub newRow()
Dim rngSh1 As Range, rngSh2 As Range, rngSh3 As Range, mySelSh2 As Range, mySelSh3 As Range
Dim lastRowSh1 As Long, lastRowSh2 As Long, lastRowSh3 As Long
Dim wb As Worksheet
Dim cell As Range
Set wb = ThisWorkbook
With wb
lastRowSh1 = Worksheets("Sheet1").Range("A" & .Rows.Count).End(xlUp).Row
lastRowSh2 = Worksheets("Sheet2").Range("A" & .Rows.Count).End(xlUp).Row
lastRowSh3 = Worksheets("Sheet3").Range("A" & .Rows.Count).End(xlUp).Row
Set rngSh1 = Worksheets("Sheet1").Range("A1:A" & lastRowSh1)
Set rngSh2 = Worksheets("Sheet2").Range("A1:A" & lastRowSh2)
Set rngSh3 = Worksheets("Sheet3").Range("A1:A" & lastRowSh3)
End With
For Each cell In rngSh2.Cells
If IsError(Application.Match(cell.Value, rngSh1, 0)) Then
If mySelSh2 Is Nothing Then
Set mySelSh2 = cell
Else
Set mySelSh2 = Union(mySelSh2, cell)
End If
End If
Next cell
If Not mySelSh2 Is Nothing Then mySelSh2.Copy Destination:=Worksheets("Sheet1").Range("A" & lastRowSh1 + 1)
For Each cell In rngSh3.Cells
If IsError(Application.Match(cell.Value, rngSh1, 0)) Then
If mySelSh3 Is Nothing Then
Set mySelSh3 = cell
Else
Set mySelSh3 = Union(mySelSh3, cell)
End If
End If
Next cell
If Not mySelSh3 Is Nothing Then mySelSh3.Copy Destination:=Worksheets("Sheet1").Range("A" & lastRowSh1 + 1)
End Sub
我已经进行了所有我能想到的调整,但是我所做的每一次更改都会出现不同的错误。 任何帮助将不胜感激。谢谢!
【问题讨论】:
-
为什么不在字典中添加所有缺失值,然后在 sheet1.Columns(1) 的末尾写下该字典中的所有值?
-
哪些错误和哪些行?
-
@GuillaumeBEDOYA 我在使用字典方面也有类似的经历;但是,我采用了相反的方法,将所有已知值添加到字典中,如果找不到键则追加。加油,伙计