+ Reply to Thread
Results 1 to 7 of 7

Compare Amt between lines and move if matched

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2009
    Location
    Manassas, VA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Compare Amt between lines and move if matched

    Hi all,

    It's just a thought and I'm not sure if it's even possible.

    Please see the sample I've attached.

    I have a report that has thousands of lines... debits & credits. Most of them offset one another, 0 balance. However, there are still many w/ missing credits that need to be identied. Instead of going thru 10,000+ lines to pull out lines w/o credits - Is there a way that I can cross check the amount between lines (1&2, 2&3, 3&4 etc etc) and if their total is 0 then I want to move them to another sheet (Matched)?

    Sorry if it's too confusing!

    Thank you,

    NTB
    Attached Files Attached Files
    Last edited by NTB; 10-29-2009 at 07:43 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Compare Amt between lines and move if matched!

    Hello NTB,

    What about these lines? Do they not match?
    02726747	10/23/2009	57			       -486.11
    02726747	10/23/2009	00003801	Name2	C	486.11
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    10-01-2009
    Location
    Manassas, VA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Compare Amt between lines and move if matched!

    Yes... the it's the amount we're looking at... so those 2 lines will get cut over to 'Matched' since the sum between them is 0

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Compare Amt between lines and move if matched!

    Hello NTB,

    What would you do when there are multiple credits applied to the same transaction?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Compare Amt between lines and move if matched

    Hello NTB,

    That was fun. It works on the data you posted, but the real test is with the actual data. Run this on a copy of the original workbook to check the results. I added a button on the Data sheet to run the macro. Here is the macro code...
    Sub CopyMatches()
    
      Dim Amount As Currency
      Dim Cell As Range
      Dim DataWks As Worksheet
      Dim Docs As Object
      Dim DocNum As Variant
      Dim MatchWks As Worksheet
      Dim NextRow As Long
      Dim Rng As Range
      Dim RngEnd As Range
      
        Set DataWks = Worksheets("Data")
        Set MatchWks = Worksheets("Matched")
        
          Set Rng = DataWks.Range("A2")
          Set RngEnd = DataWks.Cells(Rows.Count, Rng.Column).End(xlUp)
          Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, DataWks.Range(Rng, RngEnd))
          
          NextRow = MatchWks.Cells(Rows.Count, "A").End(xlUp).Row
          NextRow = IIf(NextRow < 2, 2, NextRow + 1)
          
          Set Docs = CreateObject("Scripting.Dictionary")
          Docs.CompareMode = vbTextCompare
          
           'Sort the Data in ascending order by DocNum
            Rng.Resize(ColumnSize:=6).Sort Key1:=Rng.Cells(1, 1), Order1:=xlAscending, _
                                           Header:=xlYes, MatchCase:=False, _
                                           Orientation:=xlTopToBottom, _
                                           DataOption1:=xlSortTextAsNumbers
     
            For Each Cell In Rng
              DocNum = Cell.Text
              Amount = Cell.Offset(0, 5).Value
              DocType = Cell.Offset(0, 4).Text
              If Trim(DocNum) <> "" Then
                'Has DocNum be entered?
                 If Not Docs.Exists(DocNum) Then
                   'No - Check that the DocType is blank
                    If DocType = "" Then
                       Docs.Add DocNum, Amount
                    End If
                 Else
                   'Yes - Check that the DocType is "C" and the amounts equal zero
                    If Cell.Offset(-1, 0) = DocNum And DocType = "C" Then
                       If Amount + Docs(DocNum) = 0 Then
                          'Copy the rows and clear the DocNum
                          Cell.Offset(-1, 0).Resize(2, 6).Copy MatchWks.Cells(NextRow, "A")
                          Cell.Offset(-1, 0).Resize(2, 1) = ""
                          NextRow = NextRow + 2
                       End If
                    End If
                 End If
              End If
            Next Cell
            
         'Delete the rows with no DocNum
          On Error Resume Next
          Rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
          
    End Sub
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    10-01-2009
    Location
    Manassas, VA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Compare Amt between lines and move if matched

    Just woke up and saw your response. I will test it at work, but you are truly amazing.
    Thank you for spending time on this, and I will let you know the result w/i 2 hours.

    NTB

  7. #7
    Registered User
    Join Date
    10-01-2009
    Location
    Manassas, VA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Compare Amt between lines and move if matched

    It worked! Hooray :o)

    Thanks a million!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1