Macro to compare and process lines in fixed file vs daily incoming file

sncb

Board Regular
Joined
Mar 17, 2011
Messages
168
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I am trying to build a macro based excel tracker for my orders. I have an excel file called 'Tracker' that I maintain on my laptop and another file that i receive daily called 'OpenOrders'. The structure of the lines on both files are exactly the same.

In the 'Tracker 'file I have lines that show existing open orders. Then during the day yesterday some of the orders in the system would have closed and today I would have received the updated 'OpenOrders' file. Now in the newly arrived file today, orders that were closed yesterday would therefore no longer appear in the today's file while orders that are still open in the system will of course show up.

I would like to use a macro in the 'Tracker' file to look at the newly arrived file called 'OpenOrders' and

1. Compare the rows in the 'Tracker' file lines Sheet1 with the rows in the newly arrived OpenOrders file Sheet1

2. If the order lines in the Tracker file Sheet1 do not exist in the newly arrived 'OpenOrders' file Sheet1 (meaning they have been closed in the system) then those rows in the Tracker file Sheet1 should be moved to Sheet2

3. If order lines in the Tracker file Sheet1 exist in the newly arrived 'OpenOrders' file Sheet1 (meaning they are still open in the system) then the existing rows in 'Tracker' Sheet1 should remain as they are and new lines from 'OpenOrders' should be added to the 'Tracker' file Sheet1

4. The next day when the macro is run, same action as #2 and #3 but newly closed orders should be added to the last row on Tracker file Sheet2 so that i can maintain the list of all Closed orders.

Data in both files start from A1 as shown in the screenshots.

'Tracker' file as of yesterday evening:
Tracker.xlsx
ABCDEFGHIJ
1ColAColBColCCustOrdNoOrderNoColFLnNoColHProductColJ
2InfoInfoInfoOrd1IntOrd1Info1InfoProductAInfo
3InfoInfoInfoOrd2IntOrd2Info1InfoProductBInfo
4InfoInfoInfoOrd3IntOrd3Info1InfoProductCInfo
5InfoInfoInfoOrd4IntOrd4Info1InfoProductDInfo
6InfoInfoInfoOrd5IntOrd5Info1InfoProductEInfo
Sheet1


New arrived 'OpenOrders' file this morning: (Let's say ord2 and ord3 were closed yesterday)
OpenOrders.xlsx
ABCDEFGHIJ
1ColAColBColCCustOrdNoOrderNoColFLnNoColHProductColJ
2InfoInfoInfoOrd1IntOrd1Info1InfoProductAInfo
3InfoInfoInfoOrd4IntOrd4Info1InfoProductDInfo
4InfoInfoInfoOrd5IntOrd5Info1InfoProductEInfo
5InfoInfoInfoOrd6IntOrd6Info1InfoProductMInfo
6InfoInfoInfoOrd7IntOrd7Info1InfoProductDInfo
Sheet1


After running the Macro, the expected output in the 'Tracker' file Sheet 1:
Tracker.xlsx
ABCDEFGHIJ
1ColAColBColCCustOrdNoOrderNoColFLnNoColHProductColJ
2InfoInfoInfoOrd1IntOrd1Info1InfoProductAInfo
3InfoInfoInfoOrd4IntOrd4Info1InfoProductDInfo
4InfoInfoInfoOrd5IntOrd5Info1InfoProductEInfo
5InfoInfoInfoOrd6IntOrd6Info1InfoProductMInfo
6InfoInfoInfoOrd7IntOrd7Info1InfoProductDInfo
Sheet1


and the expected output in the 'Tracker' file Sheet 2:
Tracker.xlsx
ABCDEFGHIJ
1ColAColBColCCustOrdNoOrderNoColFLnNoColHProductColJ
2InfoInfoInfoOrd2IntOrd2Info1InfoProductBInfo
3InfoInfoInfoOrd3IntOrd3Info1InfoProductCInfo
Sheet2


Thanks in advance for any input that could be provided. Also thanks to advise if I've missed out on anything.
 
Ok, please give me sometime and I will get you the files. thanks again.
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi Fuji, this is a replica of what my actual data looks in both files. Let me know please if this is ok or you need more information.

CodeNameS DateC dateSP1SP2SP3TIDFCTMS
123123-US-STATE-SUBSTATE1-Jan-245-Jan-2412345678945612121212111111111ZZOCEAN
123123-US-STATE-SUBSTATE1-Jan-245-Jan-2412345678945623232323211111111ZZOCEAN
123123-US-STATE-SUBSTATE1-Jan-245-Jan-2412345678945699999999911111111ZZOCEAN
123123-US-STATE-SUBSTATE1-Jan-245-Jan-2412345678945614141414111111111ZZOCEAN
456456-ME-STATE-SUBSTATE10-Jan-2415-Feb-2445678912332121212121222222222ZZOCEAN
456456-ME-STATE-SUBSTATE10-Jan-2415-Feb-2445678912332185858585822222222ZZOCEAN
 
Upvote 0
Just tested with EXACTLY the same data in both file and obviously nothing will happen.
And just changed one of the cells and worked as it should
Code:
Sub test()
    Dim wb As Workbook, a, b, i&, ii&, s(1), ub&
    For Each wb In Workbooks
        If wb.Name = ThisWorkbook.Name Then
            a = wb.Sheets(1).[a1].CurrentRegion.Value
        ElseIf UCase$(wb.Name) = "OPENORDERS.XLSX" Then
            b = wb.Sheets(1).[a1].CurrentRegion.Value
        End If
    Next
    If Not IsArray(b) Then MsgBox "OpenOrders.xlsx is not open": Exit Sub
    ReDim Preserve a(1 To UBound(a, 1), 1 To UBound(a, 2) + 1), b(1 To UBound(b, 1), 1 To UBound(b, 2) + 1)
    For i = 2 To Application.Max(UBound(a, 1), UBound(b, 1))
        If i <= UBound(a, 1) Then a(i, UBound(a, 2)) = Join(Application.Index(a, i, 0), Chr(2))
        If i <= UBound(b, 1) Then b(i, UBound(b, 2)) = Join(Application.Index(b, i, 0), Chr(2))
    Next
    For i = 2 To UBound(a, 1)
        If a(i, UBound(a, 2)) <> "" Then
            For ii = 2 To UBound(b, 1)
                If b(ii, UBound(b, 2)) <> "" Then
                    If a(i, UBound(a, 2)) = b(ii, UBound(b, 2)) Then
                        a(i, UBound(a, 2)) = "": b(i, UBound(b, 2)) = ""
                    End If
                End If
            Next
        End If
    Next
    With ThisWorkbook.Sheets(1)
        With .[a1].Resize(UBound(a, 1), UBound(a, 2))
            .Value = a: ub = UBound(a, 2)
            .AutoFilter UBound(a, 2), "<>"
            If .Columns(1).SpecialCells(12).Count > 1 Then
                .Offset(1).SpecialCells(12).Copy Sheets("sheet2").Range("a" & Rows.Count).End(xlUp)(2)
                .Offset(1).EntireRow.Delete
            End If
            .AutoFilter
        End With
        .Range("a" & Rows.Count).End(xlUp)(2).Resize(UBound(b, 1), UBound(b, 2)).Value = b
        a = Application.Transpose(Evaluate("row(1:" & UBound(a, 2) - 1 & ")"))
        .Columns(ub).Clear
        ReDim Preserve a(0 To UBound(a) - 1)
        With .[a1].CurrentRegion
            .Rows(2).Copy
            .Cells.PasteSpecial xlPasteFormats
            .RemoveDuplicates (a), xlNo
        End With
        Application.Goto .Cells(1)
    End With
    Sheets("sheet2").Columns(UBound(b, 2)).Clear
End Sub
 
Upvote 0
Then I am totally confused myself :confused: :)

I tried various alternatives. Thinking it could be formatting I did a copy-paste values to brand new sheets and yet the same problem. With the dummy data it seems to work perfectly so I'm confounded.

What exactly is the error saying? Maybe I could understand from the error msg and try to pinpoint the probable cause.
 
Upvote 0
Good news!!!! It worked now...dont know what it was but its ok. I just started from scratch and it worked as expected...

Great...thanks a lot for your help Fuji...Appreciate the effort and the goodwill. Wish you a good weekend.
 
Upvote 0
That's good to hear.

It sometime happens...
It worked now...dont know what it was but its ok. I just started from scratch and it worked as expected...
 
Upvote 1
Hi Fuji,

Sorry to come back on this one. When I changed the filename to the one I need, the whole Ln4 errored out - Error 424 - Object required in yellow. Im guessing its referring to the fact that the file with the name is not open but it actually is. I tried with various names but getting the same error. Any ideas why?

Ln4:
If wb.Name = Tracking.Name Then

and the file name is Tracking.xlsm
 
Upvote 0
In my real data, I took the file with like 20 lines in the Tracking file and deleted the bottom 10 lines and saved it as the OPENORDERS file and tested and then got this error. I also got this error sometimes with test data too so its random.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top