nzclogger1
New Member
- Joined
- Oct 2, 2014
- Messages
- 16
I have a userform that adds a new item to my chart of accounts.
Currently I have a userform that allows an account to be added based on four subcategories, with dependent combo boxes.
For example:
- The first combo box (called "Level1") has the options such as "assets", "liabilities", "equity", "income" etc
- The second combo box (called "Level2") has subcategories based on the above selection. E.g. if I select "assets" in the Level1 combo box, the Level2 combo box will automatically update with the options "current assets" and "non current assets". But if I select "liabilities" in the Level1 combo box, the Level2 combo box will automatically update with the options "current liabilities" and "non current liabilities". And so on with the other categories.
- There is also a Level3 combo box and a level4 combo box (e.g. current assets are divided into bank accounts, prepayments etc).
Currently I have the following code, which works perfectly well (I haven't included the full list, but I've included the general gist). As you can see, all of the categories are currently entered in manually in the macro code.
Eventually this macro will insert new categories - although all I need help with is the dependent combo boxes.
The accounts are stored in Sheet "Chart of accounts" with columns. Each account has a row, and there is a column in level order for each category.
E.g. on a single line, there would be: Account_name, then in the next column "Asset", then in the next column "Current asset", then in the next column "Bank account" etc
What I'd like, instead of manually entering in the accounts via macro code, is to create a dynamic list for each category, based on the columns in the "Chart of accounts" sheet. E.g. have code that looks in the first column and creates a unique list of the items in the Level1 category (which would produce "assets", "liabilities" etc). Then if Level1 is selected, a unique list for Level 2 is created, only if the Level2 item corresponds to Level1. E.g. even though "current assets" and "current liabilities" are technically unique items in the Level2 list, only "current assets" would show up in the Level2 combo box if "assets" was selected in the Level1 combo box, and "current liabilities" would show up in the Level2 combo box if "liabilities" was selected.
I have written the following code to create a unique list for Level1. I'm attempting to modify this for the Level2 list, but cannot figure out how to only include the items corresponding to the relevant Level1 category. Note the range "COA_column" is the column to the left of the Level1 column (as I am planning to offset it for each level - i.e. Range("COA_column").Offset(0,1) is the Level1 list and Range("COA_column").Offset(0,2) is the Level2 list etc). And .RemoveItem 0 deletes the first unique category as it is a blank cell (I only populate columns for detail accounts, not headings).
If someone could help for Level2, that would be appreciated - from there I can adapt it for Level3 and Level4.
Many thanks!
Currently I have a userform that allows an account to be added based on four subcategories, with dependent combo boxes.
For example:
- The first combo box (called "Level1") has the options such as "assets", "liabilities", "equity", "income" etc
- The second combo box (called "Level2") has subcategories based on the above selection. E.g. if I select "assets" in the Level1 combo box, the Level2 combo box will automatically update with the options "current assets" and "non current assets". But if I select "liabilities" in the Level1 combo box, the Level2 combo box will automatically update with the options "current liabilities" and "non current liabilities". And so on with the other categories.
- There is also a Level3 combo box and a level4 combo box (e.g. current assets are divided into bank accounts, prepayments etc).
Currently I have the following code, which works perfectly well (I haven't included the full list, but I've included the general gist). As you can see, all of the categories are currently entered in manually in the macro code.
Code:
'Brings up initial level 1 list if the user form is opened
Private Sub UserForm_Initialize()
With Level1
.AddItem "Assets"
.AddItem "Liabilities"
.AddItem "Equity"
.AddItem "Income"
.AddItem "Expenses"
.AddItem "Tax refund"
.AddItem "Tax"
End With
End Sub
'When level 1 is selected, the level 2 list will update
Private Sub Level1_Change()
Dim Level_1 As Integer
Level_1 = Level1.ListIndex
'Clears Level 2 combo box
Level2.Clear
'Cases refers to where the item appears in the list. Numbers start from 0. Look to code above to figure out what the name of the case is.
Select Case Level_1
Case Is = 0 'Assets
With Level2
.AddItem "Current assets"
.AddItem "Non current assets"
End With
Case Is = 1 'Liabilities
With Level2
.AddItem "Current liabilities"
.AddItem "Non current liabilities"
End With
Case Is = 2 'Equity
With Level2
.AddItem "Miscellaneous equity"
.AddItem "Reserves"
End With
Case Is = 3 'Income
With Level2
.AddItem "Miscellaneous income"
.AddItem "Employment income"
.AddItem "From people"
End With
Case Is = 4 'Expenses
With Level2
.AddItem "Miscellaneous expenses"
.AddItem "Home"
.AddItem "Car"
End With
Case Is = 6 'Tax
With Level2
.AddItem "Miscellaneous tax"
.AddItem "PAYE tax"
End With
End Select
End Sub
'When level 2 is selected, the level 3 list will update
Private Sub Level2_Change()
Dim Level_2 As Integer
Level_2 = Level2.ListIndex
'Clears Level 3 combo box
Level3.Clear
'Cases refers to where the item appears in the list. Numbers start from 0. Look to code above to figure out what the name of the case is.
'You need to jump to the box behind to ensure the correct list is being shown. Cases are still needed for the immediate list before.
If Level1.Value = "Assets" Then
Select Case Level_2
Case Is = 0 'Current assets
With Level3
.AddItem "Cash"
.AddItem "Bank accounts"
.AddItem "Foreign currency"
.AddItem "Gift cards and vouchers"
.AddItem "Accounts receivable"
.AddItem "Prepayments"
.AddItem "Accrued income"
End With
Case Is = 1 'Non current assets
With Level3
.AddItem "Fixed assets"
.AddItem "Investments"
End With
End Select
ElseIf Level1.Value = "Liabilities" Then
Select Case Level_2
Case Is = 0 'Current liabilities
With Level3
.AddItem "Miscellaneous current liabilities"
.AddItem "Accounts payable"
.AddItem "Accrued expenses"
End With
Case Is = 1 'Non current liabilities
With Level3
.AddItem "Student loan"
.AddItem "Mortgage"
End With
End Select
ElseIf Level1.Value = "Expenses" Then
Select Case Level_2
Case Is = 1 'Home
With Level3
.AddItem "Miscellaneous home"
.AddItem "*******"
.AddItem "Lounge"
.AddItem "Bedroom"
.AddItem "Bathroom"
.AddItem "Laundry"
.AddItem "Outdoor"
End With
End Select
End If
End Sub
Eventually this macro will insert new categories - although all I need help with is the dependent combo boxes.
The accounts are stored in Sheet "Chart of accounts" with columns. Each account has a row, and there is a column in level order for each category.
E.g. on a single line, there would be: Account_name, then in the next column "Asset", then in the next column "Current asset", then in the next column "Bank account" etc
What I'd like, instead of manually entering in the accounts via macro code, is to create a dynamic list for each category, based on the columns in the "Chart of accounts" sheet. E.g. have code that looks in the first column and creates a unique list of the items in the Level1 category (which would produce "assets", "liabilities" etc). Then if Level1 is selected, a unique list for Level 2 is created, only if the Level2 item corresponds to Level1. E.g. even though "current assets" and "current liabilities" are technically unique items in the Level2 list, only "current assets" would show up in the Level2 combo box if "assets" was selected in the Level1 combo box, and "current liabilities" would show up in the Level2 combo box if "liabilities" was selected.
I have written the following code to create a unique list for Level1. I'm attempting to modify this for the Level2 list, but cannot figure out how to only include the items corresponding to the relevant Level1 category. Note the range "COA_column" is the column to the left of the Level1 column (as I am planning to offset it for each level - i.e. Range("COA_column").Offset(0,1) is the Level1 list and Range("COA_column").Offset(0,2) is the Level2 list etc). And .RemoveItem 0 deletes the first unique category as it is a blank cell (I only populate columns for detail accounts, not headings).
Code:
Private Sub UserForm_Initialize()
Dim v, c
With Sheets("Chart of accounts").Range("COA_column").Offset(0, 1)
v = .Value
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each c In v
If Not .exists(c) Then .Add c, Nothing
Next
If .Count Then Me.Level1.List = Application.Transpose(.keys)
End With
With Level1
.RemoveItem 0
End With
End Sub
If someone could help for Level2, that would be appreciated - from there I can adapt it for Level3 and Level4.
Many thanks!