简体   繁体   English

如果值已经存在,VBA 宏不会将单元格复制到另一个工作表

[英]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.

相关问题 Excel宏和VBA,将单元格范围复制到另一个工作表(转置并基于另一个值) - Excel Macro & VBA, Copy Cell Range to another sheet (transposed and based on another value) VBA 根据日期将工作表 1 中的单元格值复制/粘贴到工作表 2 的宏 - VBA Macro to copy/paste cell value from sheet 1 to 2 based on date 如果单元格值大于0,excel vba将单元格复制到另一个工作表 - excel vba copy cells to another sheet if cell value is greater than 0 如何在不使用宏或VBA的情况下基于另一个工作表中的单元格值隐藏/取消隐藏工作表 - How to hide/un hide a sheet based on a cell value in another sheet without using macro or vba 通过单元格引用在另一张表中循环输入值,然后复制公式值并使用 vba 将其粘贴到另一张表中 - Loop input values in another sheet through cell reference then Copy formula value and paste it to another sheet using vba Excel 宏在工作表中查找单元格并将整行复制到另一个工作表中 - Excel macro to find a cell in a sheet and copy the entire row in another sheet VBA,Excel宏:将单元格值复制到另一个工作表,偏移,重复直到找到空单元格 - VBA, Excel macros: copy cell value to another sheet, offset, repeat until finding an empty cell Excel宏可根据单元格值将一行中的选定单元格从一张纸复制到另一张纸 - Excel Macro to copy select cells from a row from one sheet to another based upon a cell value 仅将数据从一张纸复制到另一张纸的宏(如果尚未存在) - Macro to copy data from one sheet to another, only if not already present 使用vba将单元格的值复制到另一个单元格 - Use vba to copy value of cell to another cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM