VBA to convert accounting entries into a desired format

baleshst

Board Regular
Joined
Jan 24, 2010
Messages
132
I have a excel sheet in which the debit credit entries are done between entities. Which is the source sheet which looks somewhat like this.

Copy of Mitesh(1).xlsx
ABCDEFGHIJK
1INCOMEEXPENSES
2S.No.Name FromName toINCOME GL codeConsol Grouping IncomeIncome under the HeadIncome during the year DrEXPENSES GL codeConsol Grouping ExpsExpenses under the HeadExpenses during the year Cr
31Phoenix MillsSpaces Private Limited320201-003From Financial Assests carried at fair value through profit and lossInterest214
41Spaces Private LimitedPhoenix Mills500002-004Interest ExpensesInterest214
52Phoenix MillsHospitality Services Limited320100-024License fees and Rental IncomeRent, Compensation & Other recoveries1,28,48,070420111-217Advertisement, Promotion and Marketing ExpensesService charges1,08,402
62Hospitality Services LimitedPhoenix Mills320100-003OthersOperating Income1,08,402450002-003RentRent1,28,48,070
Input


I would like the output to be like this. Request you to please give me a code to work this out. Thanks in advance.

Copy of Mitesh(1).xlsx
ABCDEFGH
1
2S.No.Name FromName toGL codeDebit/CreditINCOMEEXPENSES
31Phoenix MillsSpaces Private Limited320201-003Debit214
41Spaces Private LimitedPhoenix Mills500002-004Credit214
52Phoenix MillsHospitality Services Limited320100-024Debit12848070
62Hospitality Services LimitedPhoenix Mills450002-003Credit12848070
73Hospitality Services LimitedPhoenix Mills320100-003Debit108402
83Phoenix MillsHospitality Services Limited420111-217Credit108402
9
Output
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
According to your examples, records always come in pairs (debit and credit), so try the following, put all the code in a module and run the "Convert_Accounting_Entries" macro.

VBA Code:
Dim a As Variant, b As Variant        'At the beginning of all the code

Sub Convert_Accounting_Entries()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim i As Long, k As Long, n As Long
  
  With Sheets("Input")
    a = .Range("A3:K" & .Range("A" & Rows.Count).End(3).Row).Value
    ReDim b(1 To UBound(a, 1) * 2, 1 To 7)
  End With
    
  For i = 1 To UBound(a, 1) Step 2
    n = n + 1
    k = k + 1
    If a(i, 4) = "" Or a(i + 1, 4) = "" Then
      If a(i, 4) <> "" Then Call Proc1(a, b, i, k, n) Else Call Proc2(a, b, i, k, n)
    Else
      Call Proc1(a, b, i, k, n)
      n = n + 1
      k = k + 1
      Call Proc2(a, b, i, k, n)
    End If
  Next
  
  With Sheets("Output")
    .Range("A3:G" & Rows.Count).ClearContents
    .Range("A3").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End With
  
End Sub

Sub Proc1(a, b, i, k, n)
  b(k, 1) = n
  b(k, 2) = a(i, 2)
  b(k, 3) = a(i, 3)
  b(k, 4) = a(i, 4)
  b(k, 5) = "Debit"
  b(k, 6) = a(i, 7)
  
  k = k + 1
  b(k, 1) = n
  b(k, 2) = a(i + 1, 2)
  b(k, 3) = a(i + 1, 3)
  b(k, 4) = a(i + 1, 8)
  b(k, 5) = "Credit"
  b(k, 7) = a(i + 1, 11)
End Sub

Sub Proc2(a, b, i, k, n)
  b(k, 1) = n
  b(k, 2) = a(i + 1, 2)
  b(k, 3) = a(i + 1, 3)
  b(k, 4) = a(i + 1, 4)
  b(k, 5) = "Debit"
  b(k, 6) = a(i + 1, 7)

  k = k + 1
  b(k, 1) = n
  b(k, 2) = a(i, 2)
  b(k, 3) = a(i, 3)
  b(k, 4) = a(i, 8)
  b(k, 5) = "Credit"
  b(k, 7) = a(i, 11)
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
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