excel - 在excel vba中使用表单文本框进行重复检查
问题描述
我正在将数据从 EXCEL 用户表单存储到 Excel 工作表。我有重复检查的问题。
我在A列中的数据像这样...... 300、304、305、306、307、308、309、310等等......
例如,用户在相应的文本框中输入 306,然后消息框将显示“输入的重复数据”。
更新后我在文本框中写了以下代码,但它不起作用。任何帮助tqs...
Dim Cell As Variant
Dim Source As Range
Set Source = Worksheets("NFLAT").Range("a2:a50")
For Each Cell In Source
If Application.WorksheetFunction.CountIf(Source, Cell) > 1 Then
MsgBox "Given number already exists"
End If
Next Cell
解决方案
不需要循环,只需检查文本框的值。如果存在 CountIf > 0(不 > 1)。
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim rng As Range, ws As Worksheet, lastrow As Long
Set ws = Worksheets("NFLAT")
lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
Set rng = ws.Range("A2:A" & lastrow)
With Me.TextBox1
If Application.WorksheetFunction.CountIf(rng, .value) > 0 Then
MsgBox .value & " already exists", vbCritical, "Checked " & rng.Address
Else
MsgBox .value & " is OK ", vbInformation, "Checked " & rng.Address
End If
End With
End Sub
推荐阅读
- amazon-web-services - 在 powershell 中创建和保存函数以供全球使用
- excel - VBA web 抓取使用 Excel 和 Internet Explorer 保留前行的信息
- reactjs - 如何对以下代码中的列进行排序
- python - 如何使用 python 启动应用程序(缩放客户端)?
- spring-boot - 当注册代理关闭时,服务从 Consul 集群中完全注销
- vba - 将带有计算的文本框条目输出到Excel工作表的VBA不匹配错误?
- python - 如何在具有条件的 DataFrame 中逐行添加元素?
- mysql - 当我重新启动 docker-compose 的 mariadb 时出错
- javascript - 我的注册帖子请求不断重复
- python - Python子进程为变量赋值