[英]VBA Macro to not Copy Cell over to Another Sheet if Value Already Exists
I have a list of about 5000 contacts with their information organized by the work they perform.我有一个大约 5000 个联系人的列表,他们的信息按他们执行的工作组织。 Some are listed multiple times if they perform several types of work.
如果他们执行多种类型的工作,一些会被多次列出。 I use the list to send notifications to the contacts if they've been selected in accordance with the work they perform.
如果根据他们执行的工作选择了联系人,我会使用该列表向联系人发送通知。
I use the Email Merge function to send individual emails and so I wrote a macro to copy the contact's email over to another worksheet if they've been selected.我使用 Email 合并 function 来发送单个电子邮件,因此我编写了一个宏来将联系人的 email 复制到另一个工作表(如果它们已被选中)。
Sub CopySelectedMasterToMerge()
Dim RangeToConsider2 As Range
Dim strAddresses2 As String
Dim shtSrc As Worksheet, shtDest As Worksheet
Set shtSrc = Sheets("MASTER") 'source sheet
Set shtDest = Sheets("Email Merge") 'destination sheet
destRow = 2 'start copying to this row
Set RangeToConsider2 = Range("K4:K7000")
For Each Cell In RangeToConsider2
If Cell.Value = "a" Then
If Cell.Offset(0, 1) <> "ZZZZZZZZZ" Then
Cell.Offset(0, 6).Cells.Copy shtDest.Cells(destRow, 1)
destRow = destRow + 1
End If
End If
Next Cell
Worksheets("Email Merge").Activate
End Sub
This, along with the Email Merge, work really well except the contacts with multiple listings are receiving additional copies of the same email.这与 Email 合并一起工作得非常好,除了具有多个列表的联系人正在接收相同 email 的额外副本。
How can I prevent an email being copied over again if it already exists on the other Worksheet?如果 email 已经存在于另一个工作表上,如何防止它被再次复制?
Update - using @BigBen's suggestion, I got the WorksheetFunction.CountIf
to work.更新- 使用@BigBen 的建议,我得到了
WorksheetFunction.CountIf
的工作。
For Each Cell In RangeToConsider2
If Cell.Value = "a" Then
EmailAddress = Cell.Offset(0, 6)
If Cell.Offset(0, 1) <> "ZZZZZZZZZ" Then
CountCheck = WorksheetFunction.CountIf(SearchArea, EmailAddress)
If CountCheck < 1 Then
Cell.Offset(0, 6).Cells.Copy shtDest.Cells(destRow, 1)
destRow = destRow + 1
End If
End If
End If
Next Cell
If I understand you correctly you are copying email addresses from one sheet to another based on criteria and you want the resulting list to be unique?如果我对您的理解正确,您正在根据标准将 email 地址从一张纸复制到另一张纸,并且您希望结果列表是唯一的?
You could use an intermediate structure like a Collection or a Dictionary to test for uniqueness.您可以使用像 Collection 或 Dictionary 这样的中间结构来测试唯一性。
A Dictionary is a key-value structure where the keys must be unique.字典是键值结构,其中键必须是唯一的。 The value can be the same as the key.
该值可以与键相同。 I prefer to use a Dictionary because it has a built in function to test for the existence of a key.
我更喜欢使用字典,因为它有一个内置的 function 来测试密钥的存在。 If you used a Collection you would have to write your own:)
如果您使用 Collection,则必须自己编写:)
Basically as you run through the list of emails you test for their existence in the Dictionary and only add new entries.基本上,当您浏览电子邮件列表时,您会测试它们在字典中是否存在,并且只添加新条目。 You then use the Dictionary contents to decide who to mail to - you don't even need to copy them to a new sheet.
然后,您可以使用 Dictionary 内容来决定要邮寄给谁 - 您甚至不需要将它们复制到新工作表中。 Of course you still can if you need them for some other purpose.
当然,如果您出于其他目的需要它们,您仍然可以。
Here is an excellent tutorial on what they are and how to use them.这是一个关于它们是什么以及如何使用它们的优秀教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.