Results 1 to 4 of 4

Thread: Comparing values in cells

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Location
    msia
    Posts
    3

    Question Comparing values in cells

    I m totally new to vba excel. I m not sure if what i m planning to do could b done with vba.

    is it possible to identify value/content of 2 cells in same column which is identical (the cell position is not fixed as the data is pulled from another database where the data changes each week)and compare the subsequent columns for both items.

    sample table

    A B C
    1 Part# Measurement1 Measurement2
    2 abc 10 153
    3 abd 55 142
    4
    5
    6
    7 abc 15 160
    8 abd 45 126

    in this case is it possible to locate the identical cells (there will be only 2 duplicates) in column A (abc) and then do a comparision of the measurement 1 and 2 between the 2 rows? and it will need to do the same thing for the next item abd as well (constraint: the sequence of abc,abd,etc might vary every time the data is pulled from the database).


    I would really appreciate it some on could help me out here.
    at present i m stuck at the point, on not even knowing if this is possible and how to proceed.


    btw hope my explanation is clear.


    Thanks in advance.
    sutha

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    VB Code:
    1. Sub GetMeasDiffs()
    2.  
    3.     Dim strPart     As String   ' Part#.
    4.     Dim rngFound    As Range    ' Found range.
    5.     Dim sngDiff1    As Single   ' Difference in Measurement1 values.
    6.     Dim sngDiff2    As Single   ' Difference in Measurement2 values.
    7.     Dim r           As Long     ' Row counter.
    8.    
    9.     ' Loop through cells in column A.
    10.     For r = 2 To ActiveSheet.UsedRange.Rows.Count
    11.        
    12.         ' Get Part#.
    13.         strPart = Range("A" & r).Value
    14.        
    15.         ' Check for blank cell.
    16.         If Len(strPart) Then
    17.            
    18.             ' Find next cell with same Part#.
    19.             Set rngFound = Columns("A:A").Find( _
    20.                 What:=strPart, After:=Range("A" & r), LookIn:=xlValue, LookAt:=xlWhole, _
    21.                 SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
    22.            
    23.             ' Check for matching cell found after current cell.
    24.             ' Note: if there are more than two instances of the Part#
    25.             '       you will get multiple results for that Part# with
    26.             '       each showing the difference between the current and
    27.             '       next match.
    28.             If rngFound.Row > r Then
    29.                
    30.                 ' Calculate differences.
    31.                 sngDiff1 = Abs(Range("B" & r).Value - Range("B" & rngFound.Row).Value)
    32.                 sngDiff2 = Abs(Range("C" & r).Value - Range("C" & rngFound.Row).Value)
    33.                
    34.                 ' Display results.
    35.                 MsgBox "Part: " & strPart & vbCrLf & vbCrLf & _
    36.                        "Measurement1 Difference: " & sngDiff1 & vbCrLf & _
    37.                        "Measurement2 Difference: " & sngDiff2
    38.             End If
    39.         End If
    40.     Next r
    41.    
    42. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Location
    msia
    Posts
    3
    Thanks for the help. It is great to know that it is possible to be done.

    I tried testing the code and the only problem i am getting is it does not complete the macro -(runtime error 9 - subscript out of range) on this line:
    Set rngFound = Columns("A:A").Find( _
    What:=strPart, After:=Range("A" & r), LookIn:=xlValue, LookAt:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
    sutha

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Location
    msia
    Posts
    3
    i tried removing :
    LookIn:=xlValue, LookAt:=xlWhole,
    and tested, it seems to be working but not sure whether it will cause any other issues.

    Thanks again for the help.
    sutha

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