0% found this document useful (0 votes)
20 views6 pages

Code

The document contains a VBA code module for generating financial reports in Excel, including a Trial Balance, Income Statement, and Balance Sheet. It provides functions to clear previous reports, calculate totals, and format the output for better readability. Additionally, it includes a feature to view the General Ledger for specific accounts.

Uploaded by

ademsho81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Code

The document contains a VBA code module for generating financial reports in Excel, including a Trial Balance, Income Statement, and Balance Sheet. It provides functions to clear previous reports, calculate totals, and format the output for better readability. Additionally, it includes a feature to view the General Ledger for specific accounts.

Uploaded by

ademsho81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

'--- Main Code Module ---

Option Explicit

'=========================================================
' UI & NAVIGATION
'=========================================================
Sub ShowTransactionForm()
[Link]
End Sub

'=========================================================
' REPORT GENERATION
'=========================================================

Sub GenerateTrialBalance()
Dim dict As Object
Set dict = CreateObject("[Link]")

Dim wsCoA As Worksheet, wsJournal As Worksheet, wsReport As Worksheet


Set wsCoA = [Link]("ChartOfAccounts")
Set wsJournal = [Link]("Journal")
Set wsReport = [Link]("TrialBalance")

' --- 1. Clear previous report and set up headers ---


[Link]
With [Link]("A1:D1")
.Value = Array("Account", "Account Type", "Debit", "Credit")
.[Link] = True
.[Link] = RGB(220, 230, 241)
End With

' --- 2. Get all accounts and their types from CoA into dictionary ---
Dim lastCoARow As Long
Dim i As Long
lastCoARow = [Link]([Link], "A").End(xlUp).Row

For i = 2 To lastCoARow
' Store balance, account type, normal balance
dict([Link](i, "B").Value) = Array(0, [Link](i, "C").Value,
[Link](i, "D").Value)
Next i

' --- 3. Process the Journal ---


Dim lastJournalRow As Long
Dim currentAccount As String
Dim debit As Currency, credit As Currency
lastJournalRow = [Link]([Link], "A").End(xlUp).Row

For i = 2 To lastJournalRow
currentAccount = [Link](i, "C").Value
debit = CCur([Link](i, "D").Value)
credit = CCur([Link](i, "E").Value)

If [Link](currentAccount) Then
Dim entryData As Variant
entryData = dict(currentAccount)
' Update the balance: Balance = Balance + Debit - Credit
entryData(0) = entryData(0) + debit - credit
dict(currentAccount) = entryData
End If
Next i

' --- 4. Write data to the Trial Balance report ---


Dim r As Long: r = 2
Dim key As Variant
Dim totalDebit As Currency, totalCredit As Currency

For Each key In [Link]


Dim balance As Currency
Dim accountType As String
Dim normalBalance As String

balance = dict(key)(0)
accountType = dict(key)(1)
normalBalance = dict(key)(2)

[Link](r, "A").Value = key


[Link](r, "B").Value = accountType

If normalBalance = "Debit" Then


If balance > 0 Then [Link](r, "C").Value = balance
If balance < 0 Then [Link](r, "D").Value = -balance
Else 'Normal Balance is Credit
If balance < 0 Then [Link](r, "D").Value = -balance
If balance > 0 Then [Link](r, "C").Value = balance
End If

r = r + 1
Next key

' --- 5. Add totals and format ---


[Link](r, "B").Value = "Totals"
[Link](r, "B").[Link] = True

[Link](r, "C").Formula = "=SUM(C2:C" & r - 1 & ")"


[Link](r, "D").Formula = "=SUM(D2:D" & r - 1 & ")"

With [Link]("C" & r & ":D" & r)


.[Link] = True
.Borders(xlEdgeTop).LineStyle = xlContinuous
End With

[Link]("C:D").NumberFormat = "#,##0.00"
[Link]("A:D").AutoFit

[Link]
MsgBox "Trial Balance has been generated.", vbInformation
End Sub

Sub GenerateIncomeStatement()
Dim wsTB As Worksheet, wsReport As Worksheet
Set wsTB = [Link]("TrialBalance")
Set wsReport = [Link]("IncomeStatement")

[Link]
[Link]("A1").Value = "Income Statement"
[Link]("A1").[Link] = True
[Link]("A1").[Link] = 14

' --- Variables for calculation ---


Dim lastTBRow As Long, i As Long, r As Long
Dim totalIncome As Currency, totalExpense As Currency, netIncome As Currency

lastTBRow = [Link]([Link], "A").End(xlUp).Row


r = 3 'Start writing from row 3

' --- 1. Process Income ---


[Link](r, "A").Value = "Income"
[Link](r, "A").[Link] = True
r = r + 1

For i = 2 To lastTBRow - 1 ' -1 to skip totals row


If [Link](i, "B").Value = "Income" Then
[Link](r, "A").Value = [Link](i, "A").Value ' Account Name
[Link](r, "B").Value = [Link](i, "D").Value ' Credit
Balance
totalIncome = totalIncome + [Link](i, "D").Value
r = r + 1
End If
Next i

[Link](r, "A").Value = "Total Income"


[Link](r, "A").[Link] = True
[Link](r, "B").Value = totalIncome
[Link](r, "B").[Link] = True
r = r + 2 'Add a space

' --- 2. Process Expenses ---


[Link](r, "A").Value = "Expenses"
[Link](r, "A").[Link] = True
r = r + 1

For i = 2 To lastTBRow - 1
If [Link](i, "B").Value = "Expense" Then
[Link](r, "A").Value = [Link](i, "A").Value ' Account Name
[Link](r, "B").Value = [Link](i, "C").Value ' Debit Balance
totalExpense = totalExpense + [Link](i, "C").Value
r = r + 1
End If
Next i

[Link](r, "A").Value = "Total Expenses"


[Link](r, "A").[Link] = True
[Link](r, "B").Value = totalExpense
[Link](r, "B").[Link] = True
r = r + 2

' --- 3. Calculate and display Net Income ---


netIncome = totalIncome - totalExpense
[Link](r, "A").Value = "Net Income"
[Link](r, "A").[Link] = True
[Link](r, "B").Value = netIncome
[Link](r, "B").[Link] = True
[Link](r, "B").Borders(xlEdgeTop).LineStyle = xlDouble
' --- Formatting ---
[Link]("B").NumberFormat = "#,##0.00"
[Link]("A:B").AutoFit

[Link]
MsgBox "Income Statement has been generated.", vbInformation
End Sub

Sub GenerateBalanceSheet()
Dim wsTB As Worksheet, wsReport As Worksheet, wsIS As Worksheet
Set wsTB = [Link]("TrialBalance")
Set wsReport = [Link]("BalanceSheet")
Set wsIS = [Link]("IncomeStatement")

[Link]
[Link]("A1").Value = "Balance Sheet"
[Link]("A1").[Link] = True
[Link]("A1").[Link] = 14

Dim lastTBRow As Long, i As Long, r As Long


Dim totalAssets As Currency, totalLiabilities As Currency, totalEquity As
Currency
Dim netIncome As Currency

lastTBRow = [Link]([Link], "A").End(xlUp).Row


r = 3

' --- Get Net Income from Income Statement ---


' Find the "Net Income" label and get value from adjacent cell
On Error Resume Next
netIncome = [Link]("A").Find("Net Income", LookIn:=xlValues).Offset(0,
1).Value
On Error GoTo 0

' --- 1. Process Assets ---


[Link](r, "A").Value = "Assets"
[Link](r, "A").[Link] = True
r = r + 1

For i = 2 To lastTBRow - 1
If [Link](i, "B").Value = "Asset" Then
[Link](r, "A").Value = [Link](i, "A").Value
[Link](r, "B").Value = [Link](i, "C").Value
totalAssets = totalAssets + [Link](i, "C").Value
r = r + 1
End If
Next i

[Link](r, "A").Value = "Total Assets"


[Link](r, "B").Value = totalAssets
[Link](r, "B").[Link] = True
r = r + 2

' --- 2. Process Liabilities ---


[Link](r, "A").Value = "Liabilities"
[Link](r, "A").[Link] = True
r = r + 1
For i = 2 To lastTBRow - 1
If [Link](i, "B").Value = "Liability" Then
[Link](r, "A").Value = [Link](i, "A").Value
[Link](r, "B").Value = [Link](i, "D").Value
totalLiabilities = totalLiabilities + [Link](i, "D").Value
r = r + 1
End If
Next i

[Link](r, "A").Value = "Total Liabilities"


[Link](r, "B").Value = totalLiabilities
[Link](r, "B").[Link] = True
r = r + 2

' --- 3. Process Equity ---


[Link](r, "A").Value = "Equity"
[Link](r, "A").[Link] = True
r = r + 1

For i = 2 To lastTBRow - 1
If [Link](i, "B").Value = "Equity" Then
[Link](r, "A").Value = [Link](i, "A").Value
If [Link](i, "C").Value > 0 Then 'Debit balance like Owner's Draw
[Link](r, "B").Value = -[Link](i, "C").Value
totalEquity = totalEquity - [Link](i, "C").Value
Else 'Credit balance like Owner's Capital
[Link](r, "B").Value = [Link](i, "D").Value
totalEquity = totalEquity + [Link](i, "D").Value
End If
r = r + 1
End If
Next i

[Link](r, "A").Value = "Retained Earnings (Net Income)"


[Link](r, "B").Value = netIncome
totalEquity = totalEquity + netIncome
r = r + 1

[Link](r, "A").Value = "Total Equity"


[Link](r, "B").Value = totalEquity
[Link](r, "B").[Link] = True
r = r + 2

' --- 4. Final Totals and Check ---


[Link](r, "A").Value = "Total Liabilities and Equity"
[Link](r, "B").Value = totalLiabilities + totalEquity
[Link](r, "B").[Link] = True
[Link](r, "B").Borders(xlEdgeTop).LineStyle = xlDouble

' --- Formatting ---


[Link]("B").NumberFormat = "#,##0.00"
[Link]("A:B").AutoFit

[Link]
MsgBox "Balance Sheet has been generated.", vbInformation
End Sub

Sub ViewGeneralLedger()
Dim accountName As String
accountName = InputBox("Enter the exact Account Name to view its ledger:",
"General Ledger")

If accountName = "" Then Exit Sub

Dim wsJournal As Worksheet, wsReport As Worksheet


Set wsJournal = [Link]("Journal")
Set wsReport = [Link]("GeneralLedger")

[Link]

' --- Headers ---


[Link]("A1").Value = "General Ledger for: " & accountName
[Link]("A1").[Link] = True
[Link]("A3:F3").Value = Array("Date", "Description", "Transaction ID",
"Debit", "Credit", "Running Balance")
[Link]("A3:F3").[Link] = True

Dim lastJournalRow As Long, i As Long, r As Long


Dim runningBalance As Currency

lastJournalRow = [Link]([Link], "A").End(xlUp).Row


r = 4 'Start writing data at row 4

For i = 2 To lastJournalRow
If [Link](i, "C").Value = accountName Then
[Link](r, "A").Value = [Link](i, "B").Value 'Date
[Link](r, "B").Value = [Link](i, "F").Value
'Description
[Link](r, "C").Value = [Link](i, "A").Value 'Trans ID
[Link](r, "D").Value = [Link](i, "D").Value 'Debit
[Link](r, "E").Value = [Link](i, "E").Value 'Credit

runningBalance = runningBalance + [Link](i, "D").Value -


[Link](i, "E").Value
[Link](r, "F").Value = runningBalance

r = r + 1
End If
Next i

' --- Formatting ---


[Link]("A:F").AutoFit
[Link]("D:F").NumberFormat = "#,##0.00"
[Link]("A:A").NumberFormat = "yyyy-mm-dd"

[Link]
End Sub

You might also like