简体   繁体   中英

How to compare two named ranges in same workbook but different worksheets?

I am trying to compare the values in one named range to another named range in another table on a different worksheet.

Scope of Names: Workbook Scoped

Current Roster = WSheet1
New Roster = WSheet2
CurrentNameList = Column F of CurrentRoster
NewNameList = Column F of New Roster

Issue: Time out - Type Mismatch

If c.Value = Range("NewNameList").Value Then

Here is where I am starting:

Sub Compare()
    For Each c In Range("CurrentNameList")
        If c.Value = Range("NewNameList").Value Then
           ThisWorkbook.Worksheets("CurrentRoster").Range("CurrentNameList").Offset(, 26).Value = "Active"
        Else
            ThisWorkbook.Worksheets("Current Roster").Range("CurrentNameList").Offset(, 26).Value = "InActive"
        End If
    Next
End Sub

Thank you in advance.

If your names are workbook-scoped you can refer to the ranges like this:

    Dim wb As Workbook, c As Range, m

    Set wb = ThisWorkbook 'always scope your ranges as explicitly as you can
    
    For Each c In wb.Names("CurrentNameList").RefersToRange.Cells
        'Use Match to check if a value in is a list
        m = Application.Match(c.Value, wb.Names("NewNameList").RefersToRange, 0) 'is the value in the list?
        'Did we get a match? If not then m is an error value
        c.Offset(0, 26).Value = IIf(IsError(m), "InActive", "Active")
    Next c

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