Macro to check new lines from Masterdata and add it in another sheet based on criteria

spvsr999

New Member
Joined
Aug 2, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi There,

This forum has been helpful to me from time to time ..!! Its helping me in learning as well as writing small automation. However, I am struck at this point in the automation I am working upon.

It would be kind of anyone who could help me with this.

Thanks a ton in advance.

Scenario:

There is data in the Master sheet and General Leder Sheet. The master sheet is linked to SAP and will be refreshed entirely (The full sheet will be deleted and refreshed data pops up) every time we run a macro.
The General ledger will have lines from the Master file with an additional column (Z) "Formula".

I want a vba code that helps me to find new lines from Master sheet and add in general ledger without disturbing the column Z

To check if the line is unique or not we will use a combination of A2,D2,F2,G2,K2,P2 respectively.

In view to hide PII, I have prepared a Demo file and updating here for reference.

Hope to receive some help soon ..!!

Master Data

Tracking Trial balance.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXY
1AccountAccount NameRC NumberRC NameComp CdPD DateDocTypSAP NumberQuantitiesCurrency Amount Assignment RCHeader TextSA NumberDoc. No.Item TextPersonnel NoWBS ElementReference key 3Fiscal YearPosting periodGeneral Ledger ItemDocument DateReferenceGeneral Ledger Amount
2123456Advance2066KhaanStaff1/7/2012ZJ1111110INR34,789.78CleanupSREEKANTH1123456-Khaan - Staff020121/7/2012SREEKANTH134,789.78
3123456Advance2066KhaanStaff1/7/2012ZJ1111110INR8,215.74CleanupSREEKANTH1123456-Khaan - Staff020121/7/2012SREEKANTH18,215.74
4123456Advance3893PatwarThird Part1/7/2012ZJ1111110INR79,512.35CleanupSREEKANTH1123456-Patwar - Third Part020121/7/2012SREEKANTH179,512.35
5123456Advance3893PatwarThird Part1/7/2012ZJ1111110INR19,132.76CleanupSREEKANTH1123456-Patwar - Third Part020121/7/2012SREEKANTH119,132.76
6123456Advance8096HariManager1/7/2012ZJ1111110INR124,497.24CleanupSREEKANTH1123456-Hari - Manager020121/7/2012SREEKANTH1124,497.24
7123456Advance8096HariManager1/7/2012ZJ1111110INR54,740.41CleanupSREEKANTH1123456-Hari - Manager020121/7/2012SREEKANTH154,740.41
8123456Advance8606GandhiAVP1/7/2012ZJ1111110INR150,638.91CleanupSREEKANTH1123456-Gandhi - AVP020121/7/2012SREEKANTH1150,638.91
9123456Advance8606GandhiAVP1/7/2012ZJ1111110INR55,805.47CleanupSREEKANTH1123456-Gandhi - AVP020121/7/2012SREEKANTH155,805.47
10123456Advance8710RamcharanStaff1/7/2012ZJ1111110INR120,838.14CleanupSREEKANTH1123456-Ramcharan - Staff020121/7/2012SREEKANTH1120,838.14
11123456Advance8710RamcharanStaff1/7/2012ZJ1111110INR39,459.02CleanupSREEKANTH1123456-Ramcharan - Staff020121/7/2012SREEKANTH139,459.02
Master Data


General Ledger

Tracking Trial balance.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1AccountAccount NameRC NumberRC NameComp CdPD DateDocTypSAP NumberQuantitiesCurrency Amount Assignment RCHeader TextSA NumberDoc. No.Item TextPersonnel NoWBS ElementReference key 3Fiscal YearPosting periodGeneral Ledger ItemDocument DateReferenceGeneral Ledger AmountFORMULA
2123456Advance2066KhaanStaff1/7/2012ZJ1111110INR34,789.78CleanupSREEKANTH1123456-Khaan - Staff020121/7/2012SREEKANTH134,789.78 xyz
3123456Advance2066KhaanStaff1/7/2012ZJ1111110INR8,215.74CleanupSREEKANTH1123456-Khaan - Staff020121/7/2012SREEKANTH18,215.74 yzw
4123456Advance3893PatwarThird Part1/7/2012ZJ1111110INR79,512.35CleanupSREEKANTH1123456-Patwar - Third Part020121/7/2012SREEKANTH179,512.35 abc
5123456Advance3893PatwarThird Part1/7/2012ZJ1111110INR19,132.76CleanupSREEKANTH1123456-Patwar - Third Part020121/7/2012SREEKANTH119,132.76 bcd
6123456Advance8096HariManager1/7/2012ZJ1111110INR124,497.24CleanupSREEKANTH1123456-Hari - Manager020121/7/2012SREEKANTH1124,497.24 xyz
General Ledger


Thank you
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi,
For the code to work, delete the spaces at the beginning of " Amount" in cell K1 in your pages.
VBA Code:
Sub test()
    Dim adoCn As Object, rs As Object, strSQL$

    Set adoCn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("Adodb.RecordSet")

    adoCn.Provider = "Microsoft.ACE.OLEDB.12.0"
    adoCn.Properties("Data Source") = ThisWorkbook.FullName
    adoCn.Properties("Extended Properties") = "Excel 12.0; HDR=YES"
    adoCn.Open

    strSQL = "SELECT * FROM [Master Data$] AS M WHERE NOT EXISTS " & _
             "( SELECT * FROM [General Ledger$] AS G WHERE " & _
             "G.Account=M.Account  AND G.[RC Name]=M.[RC Name] AND " & _
             "G.[PD Date]=M.[PD Date] AND G.[DocTyp]=M.[DocTyp] AND " & _
             "G.[Amount]=M.[Amount] AND G.[Item Text]=M.[Item Text] ) "

    rs.Open strSQL, adoCn, 1, 1

    If rs.RecordCount > 0 Then
        Sheets("General Ledger").Range("A1").End(xlDown).Offset(1).CopyFromRecordset rs
    End If
    rs.Close
    adoCn.Close
    Set adoCn = Nothing
    Set rs = Nothing

End Sub
 
Upvote 0
Wow .. Amazing ... You are a saver ...!!

The code is working like how I wanted .. just one small help required ..!! the code is copying the lines but not formats could you let me know where to include that as well !!!

But seriously Thank you very much @veyselemre
 
Upvote 0
Hi,
Alternative;
VBA Code:
Sub test()
    Dim data(), sM As Worksheet, sG As Worksheet, _
        lstRow&, i&, krt$, say&

    Set sM = Sheets("Master Data")
    Set sG = Sheets("General Ledger")

    With CreateObject("Scripting.Dictionary")
        lstRow = sG.Cells(Rows.Count, 1).End(3).Row
        data = sG.Range("A2:Y" & sG.Cells(Rows.Count, 1).End(3).Row).Value

        For i = 1 To UBound(data)
            krt = Join(Application.Index(data, i, Array(1, 4, 6, 7, 11, 16)), "|")
            .Item(krt) = i + 1
        Next i

        data = sM.Range("A2:Y" & sM.Cells(Rows.Count, 1).End(3).Row).Value
        For i = 1 To UBound(data)
            krt = Join(Application.Index(data, i, Array(1, 4, 6, 7, 11, 16)), "|")
            If Not .exists(krt) Then
                say = say + 1
                'sG.Cells(lstRow + say, 1).Resize(, 25).Value = sM.Cells(i + 1, 1).Resize(, 25).Value
                'sG.Cells(lstRow + say, 1).Resize(, 25).Value = Application.Index(data, i)
                sM.Cells(i + 1, 1).Resize(, 25).Copy sG.Cells(lstRow + say, 1) 'With format
            End If
        Next i
    End With

End Sub
 
Upvote 0
Hi,
Alternative;
VBA Code:
Sub test()
    Dim data(), sM As Worksheet, sG As Worksheet, _
        lstRow&, i&, krt$, say&

    Set sM = Sheets("Master Data")
    Set sG = Sheets("General Ledger")

    With CreateObject("Scripting.Dictionary")
        lstRow = sG.Cells(Rows.Count, 1).End(3).Row
        data = sG.Range("A2:Y" & sG.Cells(Rows.Count, 1).End(3).Row).Value

        For i = 1 To UBound(data)
            krt = Join(Application.Index(data, i, Array(1, 4, 6, 7, 11, 16)), "|")
            .Item(krt) = i + 1
        Next i

        data = sM.Range("A2:Y" & sM.Cells(Rows.Count, 1).End(3).Row).Value
        For i = 1 To UBound(data)
            krt = Join(Application.Index(data, i, Array(1, 4, 6, 7, 11, 16)), "|")
            If Not .exists(krt) Then
                say = say + 1
                'sG.Cells(lstRow + say, 1).Resize(, 25).Value = sM.Cells(i + 1, 1).Resize(, 25).Value
                'sG.Cells(lstRow + say, 1).Resize(, 25).Value = Application.Index(data, i)
                sM.Cells(i + 1, 1).Resize(, 25).Copy sG.Cells(lstRow + say, 1) 'With format
            End If
        Next i
    End With

End Sub
@veyselemre,

Thanks for both the solutions. I found the second one easier to tweak according to my needs.

However, in both the solutions when I try and run the macro it is taking excel into "Not responding" and is taking longer to complete the task. This may be because I will have some 2000 lines in master data.

Is there any way to fasten the process ..!!

Thanks for the help indeed
 
Upvote 0
Hi,
try this.
VBA Code:
Sub test()

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    Dim data(), sM As Worksheet, sG As Worksheet, _
        lstRow&, i&, krt$, say&, e

    Set sM = Sheets("Master Data")
    Set sG = Sheets("General Ledger")

    With CreateObject("Scripting.Dictionary")
        lstRow = sG.Cells(Rows.Count, 1).End(3).Row
        data = sG.Range("A2:Y" & sG.Cells(Rows.Count, 1).End(3).Row).Value
        
        For i = 1 To UBound(data)
            krt = ""
            For Each e In Array(1, 4, 6, 7, 11, 16)
                krt = krt & "|" & data(i, e)
            Next e
            .Item(krt) = i + 1
        Next i

        data = sM.Range("A2:Y" & sM.Cells(Rows.Count, 1).End(3).Row).Value
        
        For i = 1 To UBound(data)
            krt = ""
            For Each e In Array(1, 4, 6, 7, 11, 16)
                krt = krt & "|" & data(i, e)
            Next e

            If Not .exists(krt) Then
                say = say + 1
                sG.Cells(lstRow + say, 1).Resize(, 25).Value = sM.Cells(i + 1, 1).Resize(, 25).Value
                ' sM.Cells(i + 1, 1).Resize(, 25).Copy sG.Cells(lstRow + say, 1)    'With format
            End If
        Next i
    
    End With

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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