excel - Excel VBA,使用循环检查现有值
问题描述
我有 70 个带有电话号码的姓名,如果有重复的电话号码并删除该号码,我如何使用 For 或 if else 语句或两者检查。提前致谢!
Sub GenerateNames()
Dim ssheet1 As Worksheet
Dim rngen As Worksheet
Dim rnsheet As Worksheet
Dim i As Integer, intValueToFind As Integer
intValueToFind = 12345
Set ssheet1 = ThisWorkbook.Sheets("Sheet1")
Set rngen = ThisWorkbook.Sheets("RnGen")
Set rnsheet = ThisWorkbook.Sheets("RandomNames")
rngen.Range("A3:A70").Copy rnsheet.Range("A3:A70")
ssheet1.Range("B3:B70").Copy rnsheet.Range("B3:B70")
For b = 1 To 70
If Cells(b, 2).Value = intValueToFind Then
MsgBox ("Found value on row " & i)
Exit Sub
End If
Next b
MsgBox ("Value not found in the range!")
End Sub
解决方案
如果要检查重复的电话号码:
Public Sub CheckForDupes()
For b = 1 To 70
For c = b + 1 To 70
If Cells(b, 2).Value = Cells(c, 2).Value Then
MsgBox ("Rows " & b & " and " & c & "are duplicates.")
End If
Next c
Next b
End Sub
推荐阅读
- shell - 分配包含变量的变量
- r - 使用 TRAVIS CI 检查 R 代码会出现错误:命令 "grep -q -R "WARNING" "${RCHECK_DIR}/00check.log"" exited with 0
- python - 当我在一个类中覆盖 __div__ 方法时,为什么在我测试代码时它不起作用?
- android - 反应原生android,在尝试获取firebase消息设备令牌时得到FIS_AUTH_ERROR
- c# - 无法将类型“int”隐式转换为“bool”Unity
- amazon-web-services - AWS Cost Explorer API - 查询兑换的积分?
- javascript - 事件源连接无法在远程主机上保持活动状态
- vue.js - 我是 Vue js 和 Vue Router 的初学者,找不到 404 页面错误
- sprite-kit - 为什么 SceneKit 材质看起来不同,即使图像相同?
- c# - c# foreach 循环迭代与查询执行时间的列表