Certainly! Below is a simple VBA code that checks if the assets, liabilities, and equity in a spreadsheet balance. This code assumes that you have three cells designated for the totals of assets, liabilities, and equity. For example, let's assume:
- Total Assets are in cell
A1
- Total Liabilities are in cell
B1
- Total Equity is in cell
C1
The code will check if Assets = Liabilities + Equity.
Here's the VBA code:
Sub CheckBalanceSheet()
Dim assets As Double
Dim liabilities As Double
Dim equity As Double
Dim message As String
' Retrieve values from the cells
assets = Range("A1").Value
liabilities = Range("B1").Value
equity = Range("C1").Value
' Check if the balance sheet balances
If assets = liabilities + equity Then
message = "The balance sheet balances."
Else
message = "The balance sheet does NOT balance."
End If
' Display the result in a message box
MsgBox message, vbInformation, "Balance Sheet Check"
' Optionally, you can write the result to a specific cell
Range("D1").Value = message
End Sub
Explanation:
-
Variables:
assets, liabilities, and equity are declared to hold the values from the respective cells.
message is a string to hold the result message.
-
Retrieve Values:
- The values from cells
A1, B1, and C1 are assigned to assets, liabilities, and equity, respectively.
-
Check Balance:
- The
If statement checks if assets equal liabilities + equity.
-
Display Result:
- A message box displays whether the balance sheet balances or not.
- Optionally, the result is also written to cell
D1.
How to Use:
- Press
ALT + F11 to open the VBA editor in Excel.
- Insert a new module by clicking
Insert > Module.
- Copy and paste the above code into the module.
- Close the VBA editor.
- Run the macro by pressing
ALT + F8, selecting CheckBalanceSheet, and clicking Run.
This code will check if the balance sheet balances and inform you via a message box and an optional cell output.