excel - 在添加到工作表之前重复发现
问题描述
大家早
有人可以告诉我我哪里出错了吗?我有一个用户表单,它将数据添加到工作表但是我希望消息框显示 B 列中的名字和 C 列中的姓氏在添加到工作表之前是否已经存在。
If WorksheetFunction.CountIf(Sheet2.Range("B:C"), Me.textbox1.Value and Me.textbox2.Value) > 0 Then
MsgBox "This staff member already exists"
Exit Sub
End If
解决方案
使用Find
和FindNext
Option Explicit
Sub CheckExists()
Dim rng As Range, firstFind As String, bExists As Boolean
Dim firstname As String, surname As String
firstname = Me.textbox1.Value
surname = Me.textbox2.Value
' find in column B
Set rng = Sheet1.Columns(2).Find(firstname, LookIn:=xlValues, lookat:=xlWhole)
If rng Is Nothing Then
' not found
Else
firstFind = rng.Address
Do
If rng.Offset(0, 1) = surname Then ' Column C
bExists = True
Exit Do
End If
Set rng = Sheet1.Columns(2).FindNext(rng)
Loop While Not rng Is Nothing And rng.Address <> firstFind
End If
If bExists Then
MsgBox firstname & " " & surname & " already exists", vbExclamation, "Row " & rng.Row
Exit Sub
End If
End Sub
推荐阅读
- iptables - iptables DNAT 不能在 2 个接口之间进行端口转发
- python - 如何访问全局服务对象(类似注入)?
- intellij-idea - IntelliJ 在重构移动时更改 npm pkg 导入路径
- java - 使用 Json 请求调用 PostMapping 时出现 NoSuchMethod 错误
- sql-server - 查询生成月份级别和年初至今级别的数据
- sql - Select语句中的Select语句是这种正确的方式
- nuxt.js - 我有 nuxt auth 模块的重定向路由问题
- ios - 为什么应用程序在ios中将pdf页面转换为高质量图像时由于内存警告而终止,在真实设备中快速
- datastax - DSE:查询超时/慢
- symfony - 当一个人有两个父母时如何持久化实体