linus2026

Untitled

Apr 22nd, 2026
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.80 KB | None | 0 0
  1. Sub New_FINAL_RECON_AUDIT_PROOF()
  2.  
  3. Dim wsPR As Worksheet
  4. Dim ws2B As Worksheet
  5. Dim wsRec As Worksheet
  6. Dim wsMatch As Worksheet
  7. Dim wsNotMatch As Worksheet
  8.  
  9. Set wsPR = Sheets("Purchase_Register")
  10. Set ws2B = Sheets("GSTR-2B_Data")
  11. Set wsRec = Sheets("Reconciliation")
  12. Set wsMatch = Sheets("Matched_Data")
  13. Set wsNotMatch = Sheets("Not_Matched")
  14.  
  15. wsRec.Cells.Clear
  16. wsMatch.Cells.Clear
  17. wsNotMatch.Cells.Clear
  18.  
  19. wsRec.Range("A1:H1") = Array("GSTIN", "Supplier", "Invoice", "Books Total", "2B Total", "Diff", "Status", "Remarks")
  20. wsMatch.Range("A1:G1") = Array("GSTIN", "Supplier", "Invoice", "Books", "2B", "Status", "Type")
  21. wsNotMatch.Range("A1:G1") = Array("GSTIN", "Supplier", "Invoice", "Books", "2B", "Status", "Reason")
  22.  
  23. With wsRec.Range("A1:H1")
  24. .Font.Bold = True
  25. .Interior.Color = RGB(0, 112, 192)
  26. .Font.Color = RGB(255, 255, 255)
  27. End With
  28. With wsMatch.Range("A1:G1")
  29. .Font.Bold = True
  30. .Interior.Color = RGB(0, 112, 192)
  31. .Font.Color = RGB(255, 255, 255)
  32. End With
  33. With wsNotMatch.Range("A1:G1")
  34. .Font.Bold = True
  35. .Interior.Color = RGB(0, 112, 192)
  36. .Font.Color = RGB(255, 255, 255)
  37. End With
  38.  
  39. wsRec.Columns("C").NumberFormat = "@"
  40. wsMatch.Columns("C").NumberFormat = "@"
  41. wsNotMatch.Columns("C").NumberFormat = "@"
  42. wsRec.Columns("C").HorizontalAlignment = xlRight
  43. wsMatch.Columns("C").HorizontalAlignment = xlRight
  44. wsNotMatch.Columns("C").HorizontalAlignment = xlRight
  45.  
  46. Dim dictBooks As Object
  47. Dim dict2B As Object
  48. Set dictBooks = CreateObject("Scripting.Dictionary")
  49. Set dict2B = CreateObject("Scripting.Dictionary")
  50.  
  51. Dim i As Long
  52. Dim key As String
  53. Dim tmp As Variant
  54.  
  55. For i = 2 To wsPR.Cells(wsPR.Rows.Count, 1).End(xlUp).Row
  56.  
  57. Dim gstPR As String
  58. Dim invPR As String
  59. Dim supplierPR As String
  60. Dim taxPR As Double
  61.  
  62. gstPR = UCase(Trim(wsPR.Cells(i, 1).Value))
  63. supplierPR = Trim(wsPR.Cells(i, 2).Value)
  64. invPR = NormalizeInv(GetInvoice_Number(CStr(wsPR.Cells(i, 3).Value)))
  65. taxPR = ToNum(wsPR.Cells(i, 6)) + ToNum(wsPR.Cells(i, 7)) + ToNum(wsPR.Cells(i, 8))
  66.  
  67. If gstPR = "" Or invPR = "" Then GoTo NextPR
  68.  
  69. key = gstPR & "|" & invPR
  70.  
  71. If dictBooks.exists(key) Then
  72. tmp = dictBooks(key)
  73. tmp(0) = tmp(0) + taxPR
  74. tmp(4) = tmp(4) + 1
  75. dictBooks(key) = tmp
  76. Else
  77. dictBooks.Add key, Array(taxPR, supplierPR, gstPR, invPR, 1)
  78. End If
  79.  
  80. NextPR:
  81. Next i
  82.  
  83. For i = 2 To ws2B.Cells(ws2B.Rows.Count, 1).End(xlUp).Row
  84.  
  85. Dim gst2B As String
  86. Dim inv2B As String
  87. Dim supplier2B As String
  88. Dim tax2B As Double
  89.  
  90. gst2B = UCase(Trim(ws2B.Cells(i, 1).Value))
  91. supplier2B = Trim(ws2B.Cells(i, 2).Value)
  92. inv2B = NormalizeInv(GetInvoice_Number(CStr(ws2B.Cells(i, 3).Value)))
  93. tax2B = ToNum(ws2B.Cells(i, 6)) + ToNum(ws2B.Cells(i, 7)) + ToNum(ws2B.Cells(i, 8))
  94.  
  95. If gst2B = "" Or inv2B = "" Then GoTo Next2B
  96.  
  97. key = gst2B & "|" & inv2B
  98.  
  99. If Not dict2B.exists(key) Then
  100. dict2B.Add key, Array(tax2B, supplier2B, gst2B, inv2B)
  101. Else
  102. tmp = dict2B(key)
  103. tmp(0) = tmp(0) + tax2B
  104. dict2B(key) = tmp
  105. End If
  106.  
  107. Next2B:
  108. Next i
  109.  
  110. Dim rowRec As Long: rowRec = 2
  111. Dim rowM As Long: rowM = 2
  112. Dim rowN As Long: rowN = 2
  113.  
  114. Dim allKeys As Object
  115. Set allKeys = CreateObject("Scripting.Dictionary")
  116.  
  117. Dim k As Variant
  118. For Each k In dictBooks.keys
  119. If Not allKeys.exists(k) Then allKeys.Add k, 1
  120. Next k
  121. For Each k In dict2B.keys
  122. If Not allKeys.exists(k) Then allKeys.Add k, 1
  123. Next k
  124.  
  125. For Each k In allKeys.keys
  126.  
  127. Dim booksVal As Double: booksVal = 0
  128. Dim val2B_ As Double: val2B_ = 0
  129. Dim supplierName As String: supplierName = ""
  130. Dim invDisplay As String: invDisplay = ""
  131. Dim gstDisplay As String: gstDisplay = ""
  132.  
  133. If dictBooks.exists(k) Then
  134. booksVal = dictBooks(k)(0)
  135. supplierName = dictBooks(k)(1)
  136. gstDisplay = dictBooks(k)(2)
  137. invDisplay = dictBooks(k)(3)
  138. End If
  139.  
  140. If dict2B.exists(k) Then
  141. val2B_ = dict2B(k)(0)
  142. If supplierName = "" Then supplierName = dict2B(k)(1)
  143. If gstDisplay = "" Then gstDisplay = dict2B(k)(2)
  144. If invDisplay = "" Then invDisplay = dict2B(k)(3)
  145. End If
  146.  
  147. Dim diff As Double: diff = booksVal - val2B_
  148. Dim status As String: status = ""
  149. Dim remark As String: remark = ""
  150. Dim mType As String: mType = ""
  151.  
  152. If dictBooks.exists(k) And dict2B.exists(k) Then
  153. If Abs(diff) <= 1 Then
  154. status = "Matched"
  155. If dictBooks(k)(4) > 1 Then
  156. remark = "OK (Split Invoice)"
  157. mType = "Split Match"
  158. Else
  159. remark = "OK"
  160. mType = "Exact Match"
  161. End If
  162. Else
  163. status = "Mismatch"
  164. remark = "Value Difference"
  165. mType = "Invoice Match"
  166. End If
  167. ElseIf dictBooks.exists(k) And Not dict2B.exists(k) Then
  168. status = "Not In 2B"
  169. remark = "Missing in GSTR-2B"
  170. ElseIf Not dictBooks.exists(k) And dict2B.exists(k) Then
  171. status = "Not Found"
  172. remark = "Missing in Books"
  173. End If
  174.  
  175. wsRec.Cells(rowRec, 1) = gstDisplay
  176. wsRec.Cells(rowRec, 2) = supplierName
  177. wsRec.Cells(rowRec, 3) = invDisplay
  178. wsRec.Cells(rowRec, 4) = booksVal
  179. wsRec.Cells(rowRec, 5) = val2B_
  180. wsRec.Cells(rowRec, 6) = diff
  181. wsRec.Cells(rowRec, 7) = status
  182. wsRec.Cells(rowRec, 8) = remark
  183.  
  184. Dim rngRow As Range
  185. Set rngRow = wsRec.Range(wsRec.Cells(rowRec, 1), wsRec.Cells(rowRec, 8))
  186. rngRow.Font.Bold = False
  187. rngRow.Font.ColorIndex = xlAutomatic
  188.  
  189. Select Case status
  190. Case "Matched"
  191. rngRow.Font.Bold = True
  192. rngRow.Font.Color = RGB(0, 0, 255)
  193. Case "Mismatch", "Not In 2B", "Not Found"
  194. rngRow.Font.Bold = True
  195. rngRow.Font.Color = RGB(255, 0, 0)
  196. End Select
  197.  
  198. If status = "Matched" Then
  199. wsMatch.Cells(rowM, 1) = gstDisplay
  200. wsMatch.Cells(rowM, 2) = supplierName
  201. wsMatch.Cells(rowM, 3) = invDisplay
  202. wsMatch.Cells(rowM, 4) = booksVal
  203. wsMatch.Cells(rowM, 5) = val2B_
  204. wsMatch.Cells(rowM, 6) = status
  205. wsMatch.Cells(rowM, 7) = mType
  206. rowM = rowM + 1
  207. Else
  208. wsNotMatch.Cells(rowN, 1) = gstDisplay
  209. wsNotMatch.Cells(rowN, 2) = supplierName
  210. wsNotMatch.Cells(rowN, 3) = invDisplay
  211. wsNotMatch.Cells(rowN, 4) = booksVal
  212. wsNotMatch.Cells(rowN, 5) = val2B_
  213. wsNotMatch.Cells(rowN, 6) = status
  214. wsNotMatch.Cells(rowN, 7) = remark
  215. rowN = rowN + 1
  216. End If
  217.  
  218. rowRec = rowRec + 1
  219.  
  220. Next k
  221.  
  222. wsRec.Columns("A:H").AutoFit
  223. wsMatch.Columns("A:G").AutoFit
  224. wsNotMatch.Columns("A:G").AutoFit
  225.  
  226. MsgBox "Reconciliation completata!" & vbNewLine & vbNewLine & _
  227. "Matched: " & rowM - 2 & vbNewLine & _
  228. "Not Matched: " & rowN - 2, vbInformation, "Report Finale"
  229.  
  230. End Sub
  231.  
  232. Function ToNum(c As Range) As Double
  233. If IsNumeric(c.Value) Then
  234. ToNum = CDbl(c.Value)
  235. Else
  236. ToNum = 0
  237. End If
  238. End Function
  239.  
  240. Function NormalizeInv(inv As String) As String
  241. inv = Trim(inv)
  242. Do While Left(inv, 1) = "0" And Len(inv) > 1
  243. inv = Mid(inv, 2)
  244. Loop
  245. NormalizeInv = inv
  246. End Function
  247.  
  248. Function GetInvoice_Number(inv As String) As String
  249.  
  250. Dim oRegex As Object
  251. Dim oMatches As Object
  252. Dim sParts() As String
  253. Dim sResult As String
  254. Dim sPart As String
  255. Dim i As Integer
  256. Dim j As Integer
  257. Dim y1 As Integer
  258. Dim y2 As Integer
  259. Dim bIsFY As Boolean
  260. Dim nCount As Integer
  261. Dim sLast As String
  262. Dim sSecLast As String
  263. Dim sRemain As String
  264.  
  265. Set oRegex = CreateObject("VBScript.RegExp")
  266. oRegex.Global = True
  267.  
  268. inv = Trim(inv)
  269. If inv = "" Then GetInvoice_Number = "": Exit Function
  270.  
  271. If InStr(inv, "/") > 0 Then
  272.  
  273. sParts = Split(inv, "/")
  274.  
  275. For i = UBound(sParts) To 0 Step -1
  276. sPart = Trim(sParts(i))
  277. bIsFY = False
  278. oRegex.Pattern = "^(\d{2,4})-(\d{2,4})$"
  279. If oRegex.Test(sPart) Then
  280. Set oMatches = oRegex.Execute(sPart)
  281. y1 = CInt(oMatches(0).SubMatches(0)) Mod 100
  282. y2 = CInt(oMatches(0).SubMatches(1)) Mod 100
  283. If y2 = (y1 + 1) Mod 100 Then bIsFY = True
  284. End If
  285. If Not bIsFY Then
  286. oRegex.Pattern = "\d+"
  287. If oRegex.Test(sPart) Then
  288. Set oMatches = oRegex.Execute(sPart)
  289. sResult = ""
  290. For j = 0 To oMatches.Count - 1
  291. sResult = sResult & oMatches(j).Value
  292. Next j
  293. GetInvoice_Number = sResult
  294. Exit Function
  295. End If
  296. End If
  297. Next i
  298.  
  299. Else
  300.  
  301. sParts = Split(inv, "-")
  302. nCount = UBound(sParts) + 1
  303. oRegex.Pattern = "^\d{2,4}$"
  304.  
  305. Do While nCount >= 2
  306. sLast = Trim(sParts(nCount - 1))
  307. sSecLast = Trim(sParts(nCount - 2))
  308. If oRegex.Test(sLast) And oRegex.Test(sSecLast) Then
  309. y1 = CInt(sSecLast) Mod 100
  310. y2 = CInt(sLast) Mod 100
  311. If y2 = (y1 + 1) Mod 100 Then
  312. nCount = nCount - 2
  313. Else
  314. Exit Do
  315. End If
  316. Else
  317. Exit Do
  318. End If
  319. Loop
  320.  
  321. sRemain = ""
  322. For i = 0 To nCount - 1
  323. If i > 0 Then sRemain = sRemain & "-"
  324. sRemain = sRemain & sParts(i)
  325. Next i
  326.  
  327. oRegex.Pattern = "\d+"
  328. If oRegex.Test(sRemain) Then
  329. Set oMatches = oRegex.Execute(sRemain)
  330. sResult = ""
  331. For j = 0 To oMatches.Count - 1
  332. sResult = sResult & oMatches(j).Value
  333. Next j
  334. GetInvoice_Number = sResult
  335. Else
  336. GetInvoice_Number = ""
  337. End If
  338.  
  339. End If
  340.  
  341. End Function
  342.  
  343.  
Advertisement
Add Comment
Please, Sign In to add comment