+ Reply to Thread
Results 1 to 5 of 5

Compare what cell is stored in a Range variable.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Compare what cell is stored in a Range variable.

    How would i get something like the following code to work? Right now it returns true no matter what, when this should always be false.

    Sub test()
    Dim tmpbox As Range
    Dim finder As Range
    
    Set tmpbox = Range("a1")
    
    Set finder = Range("a3")
    
    If tmpbox.Offset(1, 0) = finder Then
    MsgBox "True"
    Else
    MsgBox "False"
    End If
    
    End Sub
    Last edited by GaidenFocus; 07-12-2012 at 03:06 PM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Compare what cell is stored in a Range variable.

    GaidenFocus,

    Right now that code is checking the values against each other. If both A2 and A3 are blank, it will return True. To see if a cell is part of another range, you'll need to use the Intersect method.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,282

    Re: Compare what cell is stored in a Range variable.

    If tmpbox.Offset(1, 0).address(external:=True) = finder.address(external:=True) Then
    or
    If Not Intersect(tmpbox.Offset(1, 0), finder) Is Nothing Then
    since both are single cells
    Remember what the dormouse said
    Feed your head

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Compare what cell is stored in a Range variable.

    It works perfect for me.

    If tmpbox.Offset(1, 0) = finder Then
    You are here asking if the value in A2 = the value in A3.

    When I ran the code, if A2 = A3 then the msgbox gave "True" else "False"!

  5. #5
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Compare what cell is stored in a Range variable.

    Thanks romper and tiger the intersect method worked just like i needed it to. Steve, i was not trying to compare the values in the cells, i was trying to compare the range stored in the variable.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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