Results 1 to 7 of 7

Thread: [RESOLVED] Comparing values in one column

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Resolved [RESOLVED] Comparing values in one column

    Hi!

    I really need some help to this!
    I have a column in which I want to compare values. First the cell B2 need comparison with other cells and if there is a match - it colors both cells red.
    Then it's B3's turn for comparison and so on. This is what I've got so far:

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

    Set xlWS = ActiveSheet

    Dim colXLparams As New Collection
    iCompare = 3
    iCheck = 2
    Chk = 0
    Do While xlWS.Cells(iCompare, 2).Value <> ""
    Checksum = Cells(iCheck, 2)
    Compare = Cells(iCompare, 2)
    If Checksum = Compare Then GoTo Paint Else
    iCompare = iCompare + 1
    Loop
    Paint:
    MsgBox "Fejl!!", vbCritical
    With Cells(iCheck, 2).Interior
    .Color = RGB(255, 0, 0)
    End With
    With Cells(iCompare, 2).Interior
    .Color = RGB(255, 0, 0)
    End With
    End Sub


    But it only compares the first cell :s

    Help me, please

  2. #2
    Member
    Join Date
    Jun 2005
    Posts
    41

    Re: Comparing values in one column

    Sveegard,
    I'm pretty new to programming, but I think I see part of your problem.
    Your code goes straight through and hits this line:

    VB Code:
    1. Do While xlWS.Cells(iCompare, 2).Value <> ""
    2. Checksum = Cells(iCheck, 2)
    3. Compare = Cells(iCompare, 2)
    4. If Checksum = Compare Then GoTo Paint Else
    If your condition is met, it jumps to here:

    VB Code:
    1. Paint:
    2. MsgBox "Fejl!!", vbCritical
    3. With Cells(iCheck, 2).Interior
    4.     .Color = RGB(255, 0, 0)
    5. End With
    6. With Cells(iCompare, 2).Interior
    7.     .Color = RGB(255, 0, 0)
    8. End With
    9. End Sub
    And you have it exit the sub.
    So it never gets to here:
    VB Code:
    1. iCompare = iCompare + 1
    2. Loop

    It looks like your code will run until it finds one value to paint, and then quit.

    Try:
    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 Integer
    7. Dim Compare As Integer
    8. Dim Chk As Integer
    9.  
    10. Set xlWS = ActiveSheet
    11.  
    12. Dim colXLparams As New Collection
    13. iCompare = 3
    14. iCheck = 2
    15. Chk = 0
    16. Do While xlWS.Cells(iCompare, 2).Value <> ""
    17. Checksum = Cells(iCheck, 2)
    18. Compare = Cells(iCompare, 2)
    19. If Checksum = Compare Then
    20. MsgBox "Fejl!!", vbCritical
    21. With Cells(iCheck, 2).Interior
    22. .Color = RGB(255, 0, 0)
    23. End With
    24. With Cells(iCompare, 2).Interior
    25. .Color = RGB(255, 0, 0)
    26. End With
    27. iCompare = iCompare + 1
    28. Else
    29. iCompare = iCompare + 1
    30. Loop
    31.  
    32. End Sub


    Maybe that will work... good luck!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Comparing values in one column

    Thanks!

    I'll try your suggestion, I'll post yet a thread if it doesn't work.


  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Exclamation Re: Comparing values in one column

    Nope, it doesn't work.

    It says: "Loop without do"!

    Typical for MS programs - nothing works :s

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

    Re: Comparing values in one column

    Try throwing an End If in there right before the Loop command. It looks to me like it's considering your Loop command to be part of the preceding Else statement.

  6. #6
    Member
    Join Date
    Jun 2005
    Posts
    41

    Re: Comparing values in one column

    Yeah, I'm sorry Sveegaard, what Mike said. I left out the End If there. Hope that works for you.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Exclamation Re: Comparing values in one column

    Yeah but thanks for you help anyway. Just need a little thing more (see my other thread)

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