Results 1 to 16 of 16

Thread: [RESOLVED] Problem Comparing Range Values

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Resolved [RESOLVED] Problem Comparing Range Values

    Thanks to your help here at the VB forum I have almost created an excellent accounting tool; unfortunately there are still flaws. Below is the code that I have been working on. For every cell in range “RA” it adds combinations of two cells in Range “RB” until it finds the two that equal the cell in range “RA.” So far it has been about 60% successful at finding all of the combinations of two cells in range “RB” whose sum equals the cells in “RA;” but for reasons unknown to me it does not find all of them. Can someone help me figure out why it misses some and what I can do about it?


    VB Code:
    1. Range("e1").Select
    2.     Range(Selection, Selection.End(xlDown)).Select
    3.     Selection.Name = "RB"
    4.    
    5.     Range("f1").Select
    6.     Range(Selection, Selection.End(xlDown)).Select
    7.     Selection.Name = "RA"
    8.    
    9.     Dim RangeA As Range
    10.     Dim RangeB As Range
    11.     Dim CellA As Range
    12.     Dim CellB As Range
    13.     Dim CellC As Range
    14.     Set RangeA = Range("RA")
    15.     Set RangeB = Range("RB")
    16.     For Each CellA In RangeA.Cells
    17.         For Each CellB In RangeB.Cells
    18.             For Each CellC In RangeB.Cells
    19.                 If CellB + CellC = CellA Then
    20.                     CellA.ClearContents
    21.                     CellB.ClearContents
    22.                     CellC.ClearContents
    23.                  End If
    24.             Next CellC
    25.         Next CellB
    26.     Next CellA

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Problem Comparing Range Values

    It looks as though you are clearing cells after they have been "found". This could lead to some problems. For example, if one of the cells in range B is equal to one in range A, then that cell plus a "zero" cell from range C will cause a match and hence cause the cell in range B to be eliminated. Thus its partner doesn't get "found".

    For example:

    Range A: 10, 20, 30
    Range B: 4, 20, 18
    Range C: 2, 6, 10

    First run though: Finds 4+6 = 10
    Second run: Finds 20 + 0 = 20
    Third run: No more solutions, left with 30 in range A, 18 in Range B and 2 in Range C

    You could try adding them to a list as you go and stepping through, so that you can see exaclty which ones are being found and which aren't.

    HTH

    zaza

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Problem Comparing Range Values

    Thank you for your quick reply; perhaps I should have put more of an explanation in my first entry. I have tried three different ways of marking the used cells; I have tried marking them by clearing them, which now thanks to you I understand why that was not working. I also tried marking them by adding a symbol to the values, but this would through it into debug mode, because the symbol was not a number. Recently I have tried replacing the numbers with another unusual number, but it still misses several that should have been marked. If it is in fact a result of how I change the cell value, do you have a suggestion for marking the cells so that they are not used twice? Also, I cannot seem to find a pattern for the ones that it finds and misses.

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

    Re: Problem Comparing Range Values

    How about color coding the cells as you match them?

    VB Code:
    1. Sub Bots_problem()
    2. Dim RangeA As Range
    3. Dim RangeB As Range
    4. Dim CellA As Range
    5. Dim CellB As Range
    6. Dim CellC As Range
    7.    
    8.     Range("e1").Select
    9.     Range(Selection, Selection.End(xlDown)).Select
    10.     Selection.Name = "RB"
    11.    
    12.     Range("f1").Select
    13.     Range(Selection, Selection.End(xlDown)).Select
    14.     Selection.Name = "RA"
    15.    
    16.     Set RangeA = Range("RA")
    17.     Set RangeB = Range("RB")
    18.    
    19.     'Clear all colour from both ranges
    20.     RangeB.Interior.ColorIndex = xlColorIndexNone
    21.     RangeA.Interior.ColorIndex = xlColorIndexNone
    22.    
    23.     For Each CellA In RangeA.Cells
    24.         For Each CellB In RangeB.Cells
    25.             If CellB.Interior.ColorIndex <> 3 Then 'Only look for cells that haven't been color coded
    26.                 For Each CellC In RangeB.Cells
    27.                     If CellC.Interior.ColorIndex <> 3 Then 'Only look for cells that haven't been color coded
    28.                         If CellB + CellC = CellA _
    29.                         And CellB.Row <> CellC.Row Then 'Ensures that your not using the same cell
    30.                             CellA.Interior.ColorIndex = 3 'Mark cell as matched
    31.                             CellB.Interior.ColorIndex = 3 'Mark cell as matched
    32.                             CellC.Interior.ColorIndex = 3 'Mark cell as matched
    33.                          End If
    34.                      End If
    35.                 Next CellC
    36.             End If
    37.         Next CellB
    38.     Next CellA
    39.    
    40.    
    41. Set RangeA = Nothing
    42. Set RangeB = Nothing
    43. End Sub

    I've also added a clause to your final if statement to ensure that you don't "double count" a cell -
    VB Code:
    1. And CellB.Row <> CellC.Row Then
    This prevents the case where the value in a cell in RangeB is half the value of a cell in RangeA being treated as two seperate values....
    Declan

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

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Problem Comparing Range Values

    Thanks again but it still missing some combinations, how do I attach an image?

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Problem Comparing Range Values

    OK, an image should be attached showing what happens after I run the below macro. I sum up one column and then switch it to the other; the results are red and blue, the yellow is what it missed, any suggestions?
    VB Code:
    1. Sub Macro1()
    2.  
    3.     Dim RangeA As Range
    4.     Dim RangeB As Range
    5.     Dim CellA As Range
    6.     Dim CellB As Range
    7.     Dim CellC As Range
    8.    
    9.     Range("e1").Select
    10.     Range(Selection, Selection.End(xlDown)).Select
    11.     Selection.Name = "RB"
    12.    
    13.     Range("f1").Select
    14.     Range(Selection, Selection.End(xlDown)).Select
    15.     Selection.Name = "RA"
    16.    
    17.     Set RangeA = Range("RA")
    18.     Set RangeB = Range("RB")
    19.    
    20.     'Clear all colour from both ranges
    21.     RangeB.Interior.ColorIndex = xlColorIndexNone
    22.     RangeA.Interior.ColorIndex = xlColorIndexNone
    23.    
    24.     For Each CellA In RangeA.Cells
    25.         For Each CellB In RangeB.Cells
    26.             If CellB.Interior.ColorIndex <> 3 Then 'Only look for cells that haven't been color coded
    27.                 For Each CellC In RangeB.Cells
    28.                     If CellC.Interior.ColorIndex <> 3 Then 'Only look for cells that haven't been color coded
    29.                         If CellB + CellC = CellA _
    30.                         And CellB.Row <> CellC.Row Then 'Ensures that your not using the same cell
    31.                             CellA.Interior.ColorIndex = 3 'Mark cell as matched
    32.                             CellB.Interior.ColorIndex = 3 'Mark cell as matched
    33.                             CellC.Interior.ColorIndex = 3 'Mark cell as matched
    34.                          End If
    35.                      End If
    36.                 Next CellC
    37.             End If
    38.         Next CellB
    39.     Next CellA
    40.    
    41. 'Set RangeA = Nothing
    42. 'Set RangeB = Nothing
    43.  
    44. 'Set RangeA = Range("RA")
    45. 'Set RangeB = Range("RB")
    46.    
    47.     'Clear all colour from both ranges
    48.     'RangeB.Interior.ColorIndex = xlColorIndexNone
    49.     'RangeA.Interior.ColorIndex = xlColorIndexNone
    50.    
    51.     For Each CellA In RangeB.Cells
    52.         For Each CellB In RangeA.Cells
    53.             If CellB.Interior.ColorIndex <> 41 Then 'Only look for cells that haven't been color coded
    54.                 For Each CellC In RangeA.Cells
    55.                     If CellC.Interior.ColorIndex <> 41 Then 'Only look for cells that haven't been color coded
    56.                         If CellB + CellC = CellA _
    57.                         And CellB.Row <> CellC.Row Then 'Ensures that your not using the same cell
    58.                             CellA.Interior.ColorIndex = 41 'Mark cell as matched
    59.                             CellB.Interior.ColorIndex = 41 'Mark cell as matched
    60.                             CellC.Interior.ColorIndex = 41 'Mark cell as matched
    61.                          End If
    62.                      End If
    63.                 Next CellC
    64.             End If
    65.         Next CellB
    66.     Next CellA
    67. End Sub
    Attached Images Attached Images  

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

    Re: Problem Comparing Range Values

    Are these true 2 decimal places or do they have trailing decimals?

    you count try replacing "CellB + CellC = CellA" with "round(CellB,2) + round(CellC,2) = round(CellA,2)"
    Declan

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

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Problem Comparing Range Values

    Although your idea works when there are trailing numbers; apparently these columns do not have any.
    Last edited by Botillier; Sep 20th, 2005 at 01:20 PM. Reason: spelling

  9. #9
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Problem Comparing Range Values

    Hm, that may not be enough. You could try putting a break in or an extra line of code to trap it during the second loop and monitor the values of CellB and CellC as the code runs.
    If there is a number problem, you could stick in an extra line of code to multiple the number by 100, get rid of the remaining decimals (Int or Fix) and then divide by 100 again.

    Worth a shot?


    zaza

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Problem Comparing Range Values

    Thank you for your idea I am relatively new to vb so could you please give me an example of how and when I would multiply the value by a hundred and then how and when I would revert it by dividing by a hundred.

  11. #11
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Problem Comparing Range Values

    In the line

    VB Code:
    1. If CellB + CellC = CellA_

    put

    VB Code:
    1. If (Fix(100*CellB)/100) + (Fix(100*CellC)/100)= (Fix(100*CellA)/100) _

    HTH

    zaza

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Problem Comparing Range Values

    Unfortunately that did not work, it continues to look like the example that I attached above. It must be an order thing; do you know of a way to reverse the order in which vb checks each cell?

  13. #13

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Problem Comparing Range Values

    This is a very strange thing, I typed in the numbers in a new workbook, mixed up the numbers in each column and then ran the macro. Even though they are all in a different order and in a new workbook it still found and missed the same ones. However I may just be losing my mind. The yellow cells are the cells that it keeps missing, or perhaps vb is adding them wrong.
    Attached Images Attached Images  
    Last edited by Botillier; Sep 20th, 2005 at 03:20 PM. Reason: wrong picture

  14. #14
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Problem Comparing Range Values

    Hi,

    I tried your code and your initial columns and found exactly the same results as you. By stepping through and checking the values, it transpires that it is indeed a decimal point problem - every so often it returns the sum of CellB + CellC as 1*10^-13 different from CellA, and hence doesn't get a match.
    Use the following:

    Instead of

    VB Code:
    1. If CellB + CellC = CellA_

    use

    VB Code:
    1. If Abs(CellA - (CellB + CellC)) < (1 / 10000000000)_


    i.e., if the absolute value of the difference between the sum and CellA is less than a small number, assume it is OK.

    HTH

    zaza

  15. #15

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Problem Comparing Range Values

    You are a frickin genius!!!

    How were you able to step through and discover that?

  16. #16
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: [RESOLVED] Problem Comparing Range Values

    Well, many have said....

    What I did was to put a breakpoint on the line

    VB Code:
    1. For Each CellA In RangeB.Cells

    in the second loop and hover over CellA. Pressed F5 until it read 807.68, so that I knew it was the right value. Then put a breakpoint on the line

    VB Code:
    1. If CellB + CellC = CellA _

    Hovered over CellB until it read 789.84. Then hovered over CellC and stepped through using F8 until it read 17.84. This should have been accepted. It wasn't. Inserted a line at the debug point reading:

    VB Code:
    1. f=CellA - (CellB + CellC)

    and hovered over f. Found it to be small but non-zero. Concluded that this was the problem and hence it was obvious what needed to be done.

    Glad it works,

    zaza

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