excel - 如何删除重复值?
问题描述
我动态更新 A 列和 B 列中的单元格,并将每行上的两个值(使用 &)连接起来,并将值放在 C 列中。
我的目的是通过在输入两次 firstName(A 列值)和 LastName(B 列值)时检测重复名称来实现的。当我删除重复的名称,然后是第一次出现时,会弹出一个空值(在显示 msgbox 时观察到)。
有时这是一个问题,尤其是因为有时 msgbox 不会消失。即代码崩溃。
如何防止显示空值或 msgBox?我怀疑我的 if 语句有问题。
我放在工作表中的 VBA 代码
Private Sub Worksheet_Change(ByVal Target As Range)
If WorksheetFunction.CountIf(Range("c1:c12"), Target.Offset(0, 1).Value) > 1 And _
Target.Offset(0, 1).Value <> " " Then
MsgBox Target.Offset(0, 1).Value & " is a Duplicate Entry" & vbNewLine & _
" ENTER A NEW NAME", vbInformation, "Duplicate Detected"
Target.Offset(0, 0).Value = " "
Target.Offset(0, 0).Select
ElseIf WorksheetFunction.CountIf(Range("c1:c12"), Target.Offset(0, 2).Value) > 1 And _
Target.Offset(0, 1).Value <> " " Then
MsgBox Target.Offset(0, 2).Value & " is a Duplicate Entry" & vbNewLine & _
" ENTER A NEW NAME", vbInformation, "Duplicate Detected"
Target.Offset(0, 0).Value = " "
Target.Offset(0, 0).Select
Else: Exit Sub
End If
End Sub
解决方案
如果我想用
-2 -1 0
ColA ColB ColC
First1 Last1 First1Last1
First2 Last2 First2Last2
First3 Last3 First3Last3
First4 Last4
我个人会从 ColC 的条件格式开始,以标记重复的内容,以防出现问题,从而绕过消息框。
如果我确实需要一个消息框,我会设置类似于您所拥有的:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Columns(3)) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Application.CountIfs(Range("C1:C12"),Target.Value) > 1 Then 'checks for first/last name
MsgBox("The name " & Target.Offset(0,-2).Value & " " & Target.Offset(0,-1).Value & " already exists." & vbNewLine & "Please enter a new name.")
End If
End Sub
编辑1:
鉴于 colA 和 colB 的数据输入,这是否更合适?我利用了目标的行,所以负偏移量不应该被关注,因为你知道 colA 是名字,而 colB 是姓氏。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Dim r as long
r = target.row
If isempty(cells(r,1)) or isempty(cells(r,2)) then exitsub
If Application.CountIfs(Range("B1:B12"),cells(r,2).Value,Range("A1:A12"),cells(r,1).Value) > 1 Then 'checks for first/last name
MsgBox("The name " & cells(r,1).Value & " " & cells(r,2).Value & " already exists." & vbNewLine & "Please enter a new name.")
End If
End Sub
编辑2:
在验证没有值和某些值的使用时,这个宏一直在为我的测试工作(我添加了清晰的内容和 .select 所以你回到了你应该添加数据的行);我还添加了与相交相关的范围规范,以防您将诸如 first/last 之类的值添加到 a1:b12 之外的随机位置:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range(Cells(1, 1), Cells(12, 2))) Is Nothing Then Exit Sub
Dim r As Long
r = Target.Row
If IsEmpty(Cells(r, 1)) Or IsEmpty(Cells(r, 2)) Then Exit Sub
If Application.CountIfs(Range("B1:B12"), Cells(r, 2).Value, Range("A1:A12"), Cells(r, 1).Value) > 1 Then 'checks for first/last name
MsgBox ("The name " & Cells(r, 1).Value & " " & Cells(r, 2).Value & " already exists." & vbNewLine & "Please enter a new name.")
Cells(r, 1).ClearContents
Cells(r, 2).ClearContents
Cells(r, 1).Select
End If
End Sub
推荐阅读
- php - PHP数据排序
- github - 如何在 GitHub 上设置自定义扩展到语言的映射而不向 Linguist 添加新语言
- reactjs - 代理错误:无法将请求/付款从 localhost:3000 代理到 https://localhost:5000/
- java - 如何在 java 中纠正这个 For Each 循环(最终测试的输出不正确)?
- java - 列表
泛型 Java - java - 构造函数类:使用构造函数类中的其他变量设置变量(Java)
- java - 在 JWT 身份验证设置中处理刷新令牌的最可扩展/最安全的方法是什么?
- angular - 如何在不突出阿拉斯加的情况下使用 Highcharts 突出美国?
- c# - 希望通过 C# 将 WPF RTF 文本的某些部分加粗
- isabelle - 如何在 Isabelle 中将谓词转换为函数?