Results 1 to 4 of 4

Thread: [RESOLVED] Compare Range Values (Continued)

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Resolved [RESOLVED] Compare Range Values (Continued)

    The answer that I have received to my last inquiry regarding this topic was great and useful. However, now I have an idea that may be too complicated to make a reality without having to utilize hours of a professional programmer’s time. On the other hand, I may get lucky and you guys may know a simple way to do this. Like yesterday's question I have two ranges that vary in cell number. I want to compare each cell’s value in Range1 with cells’ value in Range2. Yesterday, I asked for each cell’s value to be compared for equality (see below); this is a long jump, but this time I would like VB to compare each cell’s value of range1 to two cells’ values in range2 who’s some equals the value of the cell in range1.

    So, I imagine that it would work by taking cell 1 in Range1 and comparing it to the sum of cells 1 and 2 in Range2; If the three cells’ values do not equal than it would start over. It would take cell 1 in Range1 and this time compare it to cells 1 and 3 in Range2, and so on, until all combinations were tested; after which, it would move on to cell 2 in Range1 and start the process over.

    I don’t expect anyone to know the actual code to this, but if I could get going in the correct direction that would be great.

    VB Code:
    1. Dim Range1 As Range
    2.     Dim Range2 As Range
    3.     Dim Cell1 As Range
    4.     Dim Cell2 As Range
    5.    
    6.     Set Range1 = Range("GL")
    7.     Set Range2 = Range("Stmt")
    8.    
    9.     For Each Cell1 In Range1.Cells
    10.         For Each Cell2 In Range2.Cells
    11.             If Cell1 = Cell2 Then
    12.                 Cell1.ClearContents
    13.                 Cell2.ClearContents
    14.             End If
    15.         Next Cell2
    16.     Next Cell1
    17. 'Written by DKenny adapted by Botillier
    Last edited by Botillier; Sep 15th, 2005 at 11:56 AM.

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Compare Range Values (Continued)

    I did this in a BIG rush ...

    This isn't exactly what you want, but it gets you closer ...
    Code:
    Option Explicit
    Sub Macro1()
        Dim Range1 As Range
        Dim Range2 As Range
        Dim Cell1 As Range
        Dim Cell2 As Range
        Dim Cell3 As Range
        Dim i As Integer
        Dim j As Integer
        
        Set Range1 = Range("A1:C3")
        Set Range2 = Range("D1:F5")
        
        For Each Cell1 In Range1.Cells
            i = 1
            For Each Cell2 In Range2.Cells
                j = 1
                For Each Cell3 In Range2.Cells
                    If i <> j Then
                        If Cell1 = Range2.Cells(i) + Range2.Cells(j) Then
    
                            MsgBox "Do your thing HERE!" & Cell1.Address & Cell2.Address & Cell3.Address
                            'Use "Exit For" here if you only want the first match
                        End If
                    End If
                    j = j + 1
    
                Next Cell3
                
                i = i + 1
                
            Next Cell2
        Next Cell1
    Exit Sub
    '
    End Sub
    The initialization value for "j" probably needs to be adjusted in terms of "i" to do what you want to do. You have a few more operational details to specify before the code can be completed.

    Good Learning and Good Programming!
    Last edited by Webtest; Sep 15th, 2005 at 02:36 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Compare Range Values (Continued)

    I think the innermost loop needs to be a FOR loop running from j = i + 1 to the count of cells in Range2
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Compare Range Values (Continued)

    Great! It does more than it is supposed to but at least now I know that it is possible and your code give me a lot to work with.

Posting Permissions

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



Click Here to Expand Forum to Full Width