+ Reply to Thread
Results 1 to 7 of 7

comparing two named ranges and if at least one match display a message

Hybrid View

  1. #1
    Registered User
    Join Date
    05-03-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    4

    comparing two named ranges and if at least one match display a message

    I have two ranges in the same workbook, one which is a static named range and one on a different worksheet that can have different dimensions. Each range contains values, not strings.

    I want a macro that searches the two ranges and if one or more matches is found, displays a message in a specified cell in the worksheet with the unnamed range that says, "match found" .

    thanks

  2. #2
    Registered User
    Join Date
    04-22-2013
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003/2007 & 2010
    Posts
    90

    Re: comparing two named ranges and if at least one match display a message

    Try this, renaming ranges to whatever suites your needs:
    Sub Match()
    
    For Each cell1 In Range("Named_Range_1")
        For Each cell2 In Range("D3:F7")
            If cell1.Value = cell2.Value Then
                MsgBox cell1.Address & " matches " & cell2.Address
            End If
        Next cell2
    Next cell1
    
    End Sub

  3. #3
    Registered User
    Join Date
    05-03-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: comparing two named ranges and if at least one match display a message

    That was quick. thanks. I have problems debugging. Have I not defined the variables correctly? see my amended macro:

    Sub Match()
    Dim cell1 As Long 'new line
    Dim cell2 As Long 'new line
    For Each cell1 In Range("deal")  'errors here"  For Each control variable must be Variant or Object
    
        For Each cell2 In Range("D10:1000")
            If cell1.Value = cell2.Value Then
                MsgBox cell1.Address & " matches " & cell2.Address
            End If
        Next cell2
    Next cell1
    
    End Sub
    Last edited by Leith Ross; 05-03-2013 at 04:12 PM. Reason: Add

  4. #4
    Registered User
    Join Date
    04-22-2013
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003/2007 & 2010
    Posts
    90

    Re: comparing two named ranges and if at least one match display a message

    Please put code between CODE tags. Cell1 and 2 should be dimmed as Ranges. Furthermore you made a little error in stating the range. See below in red.

    Sub Match()
    
    Dim cell1 As Range 'new line
    Dim cell2 As Range'new line
    
    For Each cell1 In Range("deal") 'errors here" For Each control variable must be Variant or Object
     For Each cell2 In Range("D10:D1000")
      If cell1.Value = cell2.Value Then
       MsgBox cell1.Address & " matches " & cell2.Address
      End If
     Next cell2
    Next cell1
    
    End Sub

  5. #5
    Registered User
    Join Date
    05-03-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: comparing two named ranges and if at least one match display a message

    Thanks. I tried the range datatype before I first replied, but it also bombs on first line referencing cell2. Sorry to be so stupid. I"m a beginner. does it have to do with not naming the active sheet?

  6. #6
    Registered User
    Join Date
    04-22-2013
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003/2007 & 2010
    Posts
    90

    Re: comparing two named ranges and if at least one match display a message

    The code works for named range and range on active sheet yet. Otherwise, instead use, e.g. for named range:
    Thisworkbook.Worksheets("WorksheetName").Range("NamedRange")
    Otherwise, what error do you get? Attach your workbook to the forum

  7. #7
    Registered User
    Join Date
    05-03-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: comparing two named ranges and if at least one match display a message

    thanks. It now works

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1