excel - 检查excel中的唯一值
问题描述
如果数据不是唯一的,我想防止在 excel 中复制粘贴数据。我使用以下宏。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedCells As Range
Dim Cell As Range
Set AffectedCells = Intersect(Target, Target.Parent.Range("A:A"))
If Not AffectedCells Is Nothing Then
For Each Cell In AffectedCells
If CountIf(Range("A:A"),A1) >2 Then
MsgBox "The data """ & Cell.Value & """ inserted in " & Cell.Address & " in column A is duplicste. We undo!", vbCritical Application.Undo 'undo insert
Exit Sub
End If
Next Cell
End If
End Sub
它仍然允许粘贴数据。
如果值重复,我尝试让 excel 不保存自身,但它仍在保存。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rngCell As Range
Dim lngLstRow As Long lngLstRow = Sheet1.UsedRange.Rows.Count
For Each rngCell In Sheet1.Range("A1:A" & lngLstRow)
If CountIf(Range("A:A"),A1) > 2 Then
MsgBox "Please enter unique value " & rngCell.Address
rngCell.Select
End If
Next
End Sub
解决方案
推荐阅读
- java - 房间数据库更新问题
- mongodb - 从 mongodb 中的多个集合中选择
- excel - Excel 2016 - 用商合并两个数据透视表
- php - Yii2 : 将原始查询转换为 ActiveRecord
- postgresql - Postgresql如何将int4range更改为numrange列类型
- reactjs - 如何阻止 React Webpack 包含来自 package.json 的脚本
- php - Wordpress 页面中的 Angular 应用程序
- typescript - 属性“原型”不存在
- ios - 自动滚动到 UITableView 底部
- arrays - 为什么Dec内存地址下降而Hex内存地址上升?