excel - 如何使用vba在excel的列中查找重复项,然后弹出一个Msgbox?
问题描述
想要在 excel 的列中查找重复项,并希望在找到 1 个重复项时弹出一个 msgbox,如果找到多个重复项,它不应该继续弹出消息。
此外,如果我可以使用两列单元格值并一起使用它来查找重复项,这也会很有帮助。
Sub ColumnDuplicates()
Dim lastRow As Long
Dim matchFoundIndex As Long
Dim iCntr As Long
lastRow = Range("A65000").End(xlUp).Row
For iCntr = 1 To lastRow
If Cells(iCntr, 1) <> "" Then
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)
If iCntr <> matchFoundIndex Then
MsgBox ("There are duplicates in Column A")
End If
End If
Next
MsgBox ("No Duplicates in Column A")
End Sub
期望打印消息说 A 列有重复项或没有重复项
解决方案
使用情况EVALUATE
如何?
Public Sub Test()
With ThisWorkbook.Sheets("Sheet1")
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
If .Evaluate("=Max(countif(A1:A" & lr & ",A1:A" & lr & "))") > 1 Then
MsgBox "Duplicates!"
Else
MsgBox "No Duplicates!"
End If
End With
End Sub
或者,参数化:
Public Sub Test(ByVal sheet As Worksheet, ByVal columnHeading As String)
With sheet
lr = .Cells(.Rows.Count, columnHeading).End(xlUp).Row
If .Evaluate("=Max(countif(" & columnHeading & "1:" & columnHeading & lr & "," & columnHeading & "1:" & columnHeading & lr & "))") > 1 Then
MsgBox "Duplicates!"
Else
MsgBox "No Duplicates!"
End If
End With
End Sub
现在你可以像这样调用它:
Test Sheet1, "A" ' find dupes in ThisWorkbook/Sheet1 in column A
Test Sheet2, "B" ' find dupes in ThisWorkbook/Sheet2 in column B
Test ActiveWorkbook.Worksheets("SomeSheet"), "Z" ' find dupes in "SomeSheet" worksheet of whatever workbook is currently active, in column Z
推荐阅读
- sql - Microsoft SSMS 中的运行总计
- python - pyarrow.lib.ArrowNotImplementedError:尚不支持从 Parquet 文件中读取结构列表:paygw_etp_typs: list
我正在使用 Great Expectations 来测试 ETL 管道中的数据。我拥有的数据文件是 Parquet 格式并且确实有一些数组,当我尝试创建新套件或尝试使用 pyarrow/fastparquet 转换为可读格式时,我遇到以下错误:
- postgresql - 我的 docker postgres 数据库容器的连接数从 11 开始?
- git - 如何使用 gitbash 命令删除 github 中的 repo?
- wordpress - Themeforest 的高级 wordpress 主题开发
- flutter - 在 Flutter 输入中将值转换为数字格式?
- java - 使用两个队列的级别顺序遍历二叉树
- xamarin.forms - 当 UICollectionViewFlowLayout.PrepareLayout() 时 Xamarin iOS 应用程序在监视器中暂停
- flutter - 如何将用户数据存储在具有共享偏好的 Flutter 应用程序中
- curl - Curl minikube apiserver 使用客户端证书进行身份验证