如何检查一个单元格是否存在于另一个工作表中的范围内,然后再引用它

时间:2018-10-23 11:06:34

标签: vba excel-vba duplicates worksheet-function

我要做的就是检查工作表“上载”中的名称(单元格B2)是否在工作表“数据”中的A列中,如果是,则返回一条消息,指出“名称已存在”。理想情况下,我希望宏将我带到该名称存在的行。任何帮助,将不胜感激。我尝试了其他线程,但是我的代码崩溃了。因此,您能否在其中添加一个部分,一旦找到“ Data”表中是否存在重复项,则将其带入“ Data”表中并找到它。

Function InRange(Range1 As Range, Range2 As Range) As Boolean
    ' returns True if Range1 is within Range2
    InRange = Not (Application.Intersect(Range1, Range2) Is Nothing)
End Function


Sub TestInRange()

Dim copySheet As Worksheet
Dim pasteSheet As Worksheet

Set copySheet = Worksheets("Upload")
Set pasteSheet = Worksheets("Data")

    If InRange(copySheet.Range("B2"), pasteSheet.Range("A2:A300")) Then
        ' code to handle that the cell is within the right range
        MsgBox "Name exists"
    Else
        ' code to handle that the cell is not within the right range
        MsgBox "Name does not exist"
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

Sub test()

With Worksheets("Data").Range("a1:a500")
    Set c = .Find(Worksheets("Upload").Range("B2"), LookIn:=xlValues)
    If Not c Is Nothing Then
        MsgBox ("Name Exists")
            Else
        ' code to handle that the active cell is not within the right range
        MsgBox "Name does not exist"
    End If

    End With

End Sub

实际上非常简单