Results 1 to 3 of 3

Thread: [RESOLVED] Compare range values

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Resolved [RESOLVED] Compare range values

    I am still a nubi at VB and I am probably getting in over my head but, I think that you can help me figure this one out.

    I have two ranges one is named “GL” the other “Stmt.” I need to compare each cell value of range “GL” to each cell value of range “Stmnt.” Below is a sorry example of what I am trying to do. As you can see range “GL” can vary in length and each cell can be compared to the cells in range “Stmt,” but at the moment I have to write out each cell in range “Stmt,” therefore in my code range “Stmnt” cannot very in length.” How can I compare each value in range “GL” to each value in range “Stmnt” when both vary in length? I do not have a problem selecting and naming the ranges when they vary only comparing the values.

    VB Code:
    1. Sub Macro1()
    2.     Range("a1:a15").Select
    3.     Selection.Name = "GL"
    4.     Range("b1:b10").Select
    5.     Selection.Name = "Stmt"
    6.    
    7.     For Each cell In Range("GL")
    8.         If cell.Value = Range("b1") Then
    9.         cell.Interior.ColorIndex = 37
    10.         Range("b1").Interior.ColorIndex = 36
    11.         End If
    12.         If cell.Value = Range("b2") Then
    13.         cell.Interior.ColorIndex = 37
    14.         Range("b2").Interior.ColorIndex = 36
    15.         End If
    16.         If cell.Value = Range("b3") Then
    17.         cell.Interior.ColorIndex = 37
    18.         Range("b3").Interior.ColorIndex = 36
    19.         End If
    20.         If cell.Value = Range("b4") Then
    21.         cell.Interior.ColorIndex = 37
    22.         Range("b4").Interior.ColorIndex = 36
    23.         End If
    24.         If cell.Value = Range("b5") Then
    25.         cell.Interior.ColorIndex = 37
    26.         Range("b5").Interior.ColorIndex = 36
    27.         End If
    28.         If cell.Value = Range("b6") Then
    29.         cell.Interior.ColorIndex = 37
    30.         Range("b6").Interior.ColorIndex = 36
    31.         End If
    32.         If cell.Value = Range("b7") Then
    33.         cell.Interior.ColorIndex = 37
    34.         Range("b7").Interior.ColorIndex = 36
    35.         End If
    36.         If cell.Value = Range("b8") Then
    37.         cell.Interior.ColorIndex = 37
    38.         Range("b8").Interior.ColorIndex = 36
    39.         End If
    40.         If cell.Value = Range("b9") Then
    41.         cell.Interior.ColorIndex = 37
    42.         Range("b9").Interior.ColorIndex = 36
    43.         End If
    44.         If cell.Value = Range("b10") Then
    45.         cell.Interior.ColorIndex = 37
    46.         Range("b10").Interior.ColorIndex = 36
    47.         End If
    48.     Next cell
    49. End Sub

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Compare range values

    loop through each cell in the first range and compare it with each cell in the 2nd range....


    VB Code:
    1. Sub QuickAndDirty()
    2.     Dim Range1 As Range
    3.     Dim Range2 As Range
    4.     Dim Cell1 As Range
    5.     Dim Cell2 As Range
    6.    
    7.     Set Range1 = Range("Rng1") 'Replace Rng1 with your 1st range name
    8.     Set Range2 = Range("Rng2") 'Replace Rng2 with your 2nd range name
    9.    
    10.     For Each Cell1 In Range1.Cells
    11.         For Each Cell2 In Range2.Cells
    12.             If Cell1 = Cell2 Then
    13.                 Cell1.Interior.ColorIndex = 36
    14.                 Cell2.Interior.ColorIndex = 36
    15.             End If
    16.         Next Cell2
    17.     Next Cell1
    18.    
    19. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Compare range values

    Awesome!

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