SreerajMavila

AUTOMATE RECONCILIATION

Aug 23rd, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 5.01 KB | Software | 0 0
  1. Sub GenerateSummaryReport()
  2.     Dim wsSupplier As Worksheet
  3.     Dim wsNS As Worksheet
  4.     Dim wsSummary As Worksheet
  5.     Dim lastRowSupplier As Long
  6.     Dim lastRowNS As Long
  7.     Dim totalSupplierAmount As Double
  8.     Dim totalNSAmount As Double
  9.     Dim i As Long, j As Long
  10.     Dim summaryRow As Long
  11.     Dim foundMatch As Boolean
  12.     Dim supplierAmount As Double
  13.     Dim nsAmount As Double
  14.     Dim discrepancyTotal As Double
  15.    
  16.     ' Set the worksheets
  17.    Set wsSupplier = ThisWorkbook.Sheets("Supplier SOA")
  18.     Set wsNS = ThisWorkbook.Sheets("NS SOA")
  19.     Set wsSummary = ThisWorkbook.Sheets("Summary")
  20.    
  21.     ' Clear the Summary sheet
  22.    wsSummary.Cells.Clear
  23.    
  24.     ' Calculate total amounts
  25.    totalSupplierAmount = Application.WorksheetFunction.Sum(wsSupplier.Range("C2:C" & wsSupplier.Cells(wsSupplier.Rows.Count, "C").End(xlUp).Row))
  26.     totalNSAmount = Application.WorksheetFunction.Sum(wsNS.Range("C2:C" & wsNS.Cells(wsNS.Rows.Count, "C").End(xlUp).Row))
  27.    
  28.     ' Display totals and overall difference
  29.    wsSummary.Cells(1, 1).Value = "Total Invoice Amount of Supplier SOA"
  30.     wsSummary.Cells(1, 2).Value = totalSupplierAmount
  31.    
  32.     wsSummary.Cells(2, 1).Value = "Total Invoice Amount of NS SOA"
  33.     wsSummary.Cells(2, 2).Value = totalNSAmount
  34.    
  35.     wsSummary.Cells(3, 1).Value = "Overall Difference"
  36.     wsSummary.Cells(3, 2).Value = totalNSAmount - totalSupplierAmount
  37.    
  38.     ' Initialize discrepancy total
  39.    discrepancyTotal = 0
  40.    
  41.     ' Set header for differences table
  42.    summaryRow = 5
  43.     wsSummary.Cells(summaryRow, 1).Value = "Date"
  44.     wsSummary.Cells(summaryRow, 2).Value = "Invoice Number"
  45.     wsSummary.Cells(summaryRow, 3).Value = "Supplier Amount"
  46.     wsSummary.Cells(summaryRow, 4).Value = "NS Amount"
  47.     wsSummary.Cells(summaryRow, 5).Value = "Difference in Amount"
  48.    
  49.     ' Find invoices in both SOAs with amount differences
  50.    summaryRow = 6
  51.     For i = 2 To wsSupplier.Cells(wsSupplier.Rows.Count, "A").End(xlUp).Row
  52.         foundMatch = False
  53.         For j = 2 To wsNS.Cells(wsNS.Rows.Count, "A").End(xlUp).Row
  54.             If wsSupplier.Cells(i, 2).Value = wsNS.Cells(j, 2).Value Then
  55.                 supplierAmount = Val(wsSupplier.Cells(i, 3).Value)
  56.                 nsAmount = Val(wsNS.Cells(j, 3).Value)
  57.                 If supplierAmount <> nsAmount Then
  58.                     wsSummary.Cells(summaryRow, 1).Value = wsSupplier.Cells(i, 1).Value
  59.                     wsSummary.Cells(summaryRow, 2).Value = wsSupplier.Cells(i, 2).Value
  60.                     wsSummary.Cells(summaryRow, 3).Value = supplierAmount
  61.                     wsSummary.Cells(summaryRow, 4).Value = nsAmount
  62.                     wsSummary.Cells(summaryRow, 5).Value = nsAmount - supplierAmount
  63.                     discrepancyTotal = discrepancyTotal + (nsAmount - supplierAmount)
  64.                     summaryRow = summaryRow + 1
  65.                 End If
  66.                 foundMatch = True
  67.                 Exit For
  68.             End If
  69.         Next j
  70.     Next i
  71.    
  72.     ' Find invoices in Supplier SOA but not in NS SOA
  73.    For i = 2 To wsSupplier.Cells(wsSupplier.Rows.Count, "A").End(xlUp).Row
  74.         foundMatch = False
  75.         For j = 2 To wsNS.Cells(wsNS.Rows.Count, "A").End(xlUp).Row
  76.             If wsSupplier.Cells(i, 2).Value = wsNS.Cells(j, 2).Value Then
  77.                 foundMatch = True
  78.                 Exit For
  79.             End If
  80.         Next j
  81.        
  82.         If Not foundMatch Then
  83.             wsSummary.Cells(summaryRow, 1).Value = wsSupplier.Cells(i, 1).Value
  84.             wsSummary.Cells(summaryRow, 2).Value = wsSupplier.Cells(i, 2).Value
  85.             wsSummary.Cells(summaryRow, 3).Value = Val(wsSupplier.Cells(i, 3).Value)
  86.             wsSummary.Cells(summaryRow, 4).Value = "Not in NS SOA"
  87.             wsSummary.Cells(summaryRow, 5).Value = Val(wsSupplier.Cells(i, 3).Value) ' Since NS Amount is missing
  88.            discrepancyTotal = discrepancyTotal + Val(wsSupplier.Cells(i, 3).Value)
  89.             summaryRow = summaryRow + 1
  90.         End If
  91.     Next i
  92.    
  93.     ' Find invoices in NS SOA but not in Supplier SOA
  94.    For j = 2 To wsNS.Cells(wsNS.Rows.Count, "A").End(xlUp).Row
  95.         foundMatch = False
  96.         For i = 2 To wsSupplier.Cells(wsSupplier.Rows.Count, "A").End(xlUp).Row
  97.             If wsNS.Cells(j, 2).Value = wsSupplier.Cells(i, 2).Value Then
  98.                 foundMatch = True
  99.                 Exit For
  100.             End If
  101.         Next i
  102.        
  103.         If Not foundMatch Then
  104.             wsSummary.Cells(summaryRow, 1).Value = wsNS.Cells(j, 1).Value
  105.             wsSummary.Cells(summaryRow, 2).Value = wsNS.Cells(j, 2).Value
  106.             wsSummary.Cells(summaryRow, 3).Value = "Not in Supplier SOA"
  107.             wsSummary.Cells(summaryRow, 4).Value = Val(wsNS.Cells(j, 3).Value)
  108.             wsSummary.Cells(summaryRow, 5).Value = Val(wsNS.Cells(j, 3).Value) ' Since Supplier Amount is missing
  109.            discrepancyTotal = discrepancyTotal + Val(wsNS.Cells(j, 3).Value)
  110.             summaryRow = summaryRow + 1
  111.         End If
  112.         Next
  113.        End Sub
  114.        
  115.  
Tags: Vba code
Advertisement
Add Comment
Please, Sign In to add comment