简体   繁体   中英

Excel VBA, check for existing value with loop

i have a 70 names with phone numbers, how can i check with For or if else statement or both if there is phone number which is duplicating and remove the number. Thanks in advance!

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

If you want to check for duplicate phone numbers:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM