简体   繁体   English

如何检查单元格是否存在于不同工作表的范围内,然后引用它

[英]How to check if a cell exists within a range in a different worksheet and then reference it

All I want to do is check if the Name (Cell B2) in Worksheet "Upload" is in the worksheet "Data" in column A, and if so return a message saying "Name already exists".我想要做的就是检查工作表“上传”中的名称(单元格 B2)是否在 A 列的工作表“数据”中,如果是,则返回一条消息说“名称已经存在”。 Ideally I would like the macro to then take me to the row where the name exists.理想情况下,我希望宏将我带到名称所在的行。 Any help would be appreciated.任何帮助,将不胜感激。 I've tried other threads and my code just falls over.我试过其他线程,我的代码就失败了。 So could you please add in a section that once locating if the duplicate exists in the "Data" sheet then it takes me into the "Data" sheet and locates it.因此,您能否添加一个部分,一旦找到“数据”表中是否存在重复项,它就会将我带入“数据”表并找到它。

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
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

Actually quite straightforward其实很简单

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果不同工作表上相同范围内的相应单元格已经着色,如何为一个范围内的单元格着色? - How to color a cell within a range, if the corresponding cell within an identical range on a different worksheet is already colored? 如何在''工作表标题中引用单元格 - How to reference cell within '' worksheet title 如果已经在不同工作表上的相同范围内的相同单元格已经着色,则可以为一个范围内的单元格着色? - Coloring a cell within a range, if the same cell within an identical range on a different worksheet is already colored? 如何检查工作表是否存在? - How to check if worksheet exists? 引用单元格到不同工作表中的范围 - referencing a cell to a range in different worksheet 下标超出范围-如何分配变量以引用工作表中的单元格 - Subscript out of range - how to assign variable to reference the cell in a worksheet 如何检查一个范围内的单元格是否在另一个范围内 - How to check if a cell in one range exists in another range 检查列中另一个工作表上是否存在Excel单元格 - 并返回其他列的内容 - Check if an excel cell exists on another worksheet in a column - and return the contents of a different column 将范围复制到不同工作表中的下一个可用单元格 - Copy range to next available cell in different Worksheet 引用来自不同工作表的第n个单元格 - Reference every nth cell from a different worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM