Results 1 to 9 of 9

Thread: [RESOLVED] Comparison probs

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Resolved [RESOLVED] Comparison probs

    Hey

    I need some help about comparison in VBA/Excel. This is the code I've got (with a little help from bartender):

    Sub Drwcheck()
    Dim iCompare As Long
    Dim iCheck As Long
    Dim XL As Variant
    Dim xlWS As Variant
    Dim Checksum As Long
    Dim Compare As Long

    Set xlWS = ActiveSheet

    Dim colXLparams As New Collection
    iCompare = 3
    iCheck = 2

    Do While xlWS.Cells(iCompare, 2).Value <> ""
    Checksum = Cells(iCheck, 2)
    Compare = Cells(iCompare, 2)
    If Checksum = Compare Then
    With Cells(iCheck, 2).Interior
    .Color = RGB(255, 0, 0)
    End With
    With Cells(iCompare, 2).Interior
    .Color = RGB(255, 0, 0)
    End With
    iCompare = iCompare + 1
    Else
    iCompare = iCompare + 1
    End If
    Loop
    End Sub

    But it doesn't continue to the next cell and compare the rest and so on

  2. #2
    Junior Member
    Join Date
    Jul 2005
    Posts
    20

    Re: Comparison probs

    I apologize for my reply in your other topic. It was first thing in the morning and I thought I could do some good. I didn't bother to think that if you had left out an End If, VBA would have never compiled and you would have other errors. Any how, here's my suggestion..

    VB Code:
    1. Sub Drwcheck()
    2. Dim iCompare As Long
    3. Dim iCheck As Long
    4. Dim XL As Variant
    5. Dim xlWS As Variant
    6. Dim Checksum As Long
    7. Dim Compare As Long
    8.  
    9. Set xlWS = ActiveSheet
    10.  
    11. Dim colXLparams As New Collection
    12. iCompare = 3
    13. iCheck = 2
    14.  
    15. Do While xlWS.Cells(iCheck, 2).Value <> ""
    16.     Do While xlWS.Cells(iCompare, 2).Value <> ""
    17.         Checksum = Cells(iCheck, 2)
    18.         Compare = Cells(iCompare, 2)
    19.         If Checksum = Compare Then
    20.             With Cells(iCheck, 2).Interior
    21.             .Color = RGB(255, 0, 0)
    22.             End With
    23.             With Cells(iCompare, 2).Interior
    24.             .Color = RGB(255, 0, 0)
    25.             End With
    26.             iCompare = iCompare + 1
    27.         Else
    28.             iCompare = iCompare + 1
    29.         End If
    30.     Loop
    31. iCompare = iCheck + 2
    32. iCheck = iCheck + 1
    33.  
    34. Loop
    35. End Sub

    You have to have a second loop to iterate through the check values. I used iCompare = iCheck + 2 and iCheck = iCheck + 1 to avoid repeat checking. This way the new check value will be the next in the list and the compare value will be one after that.
    I tried iCompare = iCheck and iCheck = iCheck + 1 but the iCompare and iCheck eventually become the same value and the program compares a value to itself; everything becomes red.

    Man, is it Friday yet?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Exclamation Re: Comparison probs

    Thanks for that!

    Now it works properly - but one more thing:
    How can I make the fields white/transperant again when there no longer is a match?

  4. #4
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Comparison probs

    Unless I'm reading the code wrong, you just want to highlight any cell with a match, right? And when there is no longer a match, you want the cell to unhighlight?

    You don't need VBA for this. Just use conditional formatting.

    Click on Format --> Conditional Formatting in the top cell of your column. I'll assume A1

    Change the Condition to read "Cell Value Is -- Equal To" like this:


    in the box to the right of that, insert this formula
    =IF(COUNTIF(OFFSET($A$1,0,0,COUNTA($A:$A),1),A1)>1,A1,FALSE)
    This is assuming your start cell is A1, and you are using all of column A. Obviously change $A:$A to match your range, and all A1s and $A$1s to match your top cell.

    Next click the format button and select what you want your cells to look like when highlighted. Click ok.

    Now highlight cell A1 and click your format painter

    Use that to paint your format to the rest of the column.

    Now any cell with a match should be highlighted. Change the matching number and the formatting with change back to transparent.


    Forgive the explicit instructions, some may not know conditional formatting.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Exclamation Re: Comparison probs

    Hi

    I know that, but that's not what I'm looking for. I need it to be automatic and to be in the same macro as the highlight-part

  6. #6
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Comparison probs

    It is automatic, and what I'm saying is you don't even need the highlight part of the macro, you can delete it. That conditional formatting will do all the highlighting when there is a match and all unhighlighting immediately when the match is gone without using code.


    Check column A in this attachment Change any of the numbers to make matches. If this is not what you're looking for then maybe I am misunderstanding.

    I also changed the formula to disregard blank cells

    =IF(AND(COUNTIF(OFFSET($A$1,0,0,COUNTA($A:$A),1),A1)>1,A1<>0),A1,"isFalse")
    Attached Files Attached Files

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Comparison probs

    But - you need to write it in a cell, right? It has to be fool-proof, so you can't delete it by accident

  8. #8
    Junior Member
    Join Date
    Jul 2005
    Posts
    20

    Re: Comparison probs

    You could try adding another With Cells(...) statement to set the color to white in the Else portion.

    VB Code:
    1. If Checksum = Compare Then
    2.             With Cells(iCheck, 2).Interior
    3.             .Color = RGB(255, 0, 0)
    4.             End With
    5.             With Cells(iCompare, 2).Interior
    6.             .Color = RGB(255, 0, 0)
    7.             End With
    8.             iCompare = iCompare + 1
    9.         Else
    10.             With Cells(iCompare, 2).Interior
    11.             .Color = RGB(0, 0, 0)
    12.             iCompare = iCompare + 1
    13.         End If

    Is RGB (0, 0, 0,) the value for white? I don't know. You get the idea though.

    I suppose that this technically changes every non-matching cell to white, but overkill never hurt anyone.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Resolved Re: Comparison probs : Solved

    Hi all

    Thanks for all your inputs - my sheet is working properly now


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