VBA Loops

ryancgarrett

Board Regular
Joined
Jun 18, 2011
Messages
122
I have a userform with a listbox and option buttons that populate the listbox from a chart of accounts sheet. Right now my code works and this is what it looks like:

Sub PopulateListBox()
If frmAddAccount.optAsset.Value = True Then
frmAddAccount.lbxAccountName.RowSource = "'Chart of Accounts'!B3:B17"
ElseIf frmAddAccount.optLiability.Value = True Then
frmAddAccount.lbxAccountName.RowSource = "'Chart of Accounts'!B19:B23"
ElseIf frmAddAccount.optOwnerEquity.Value = True Then
frmAddAccount.lbxAccountName.RowSource = "'Chart of Accounts'!B25:B27"
ElseIf frmAddAccount.optRevenue.Value = True Then
frmAddAccount.lbxAccountName.RowSource = "'Chart of Accounts'!B30:B33"
ElseIf frmAddAccount.optExpense.Value = True Then
frmAddAccount.lbxAccountName.RowSource = "'Chart of Accounts'!B35:B53"
End If
End Sub

My problem is that if the user adds more accounts to the program then the range added to the listbox is thrown off. Each account type is assigned a different number, i.e. Assets are between 100 and 199, Liabilities are between 200 and 299 etc. Can I run a loop that determines the range of all asset accounts and then populates the listbox with those accounts if that option is selected, rather than manually defining the range as I have above.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
For what you describe using the option buttons, the basic code structure might look something like this:

Code:
Sub Populate_ListBox()
    Dim cell As Range, CoA As Range
    
    Set cell = Sht_CoA.Range("B3")
    Set CoA = Sht_CoA.Range(cell, cell.End(xlDown))
    
    With frmAddAccount
        .lbxAccountName.Clear
        For Each cell In CoA
            Select Case cell.Value
                Case 100 To 199
                    If .optAsset Then .lbxAccountName.AddItem cell.Value
                Case 200 To 299
                    If .optLiability Then .lbxAccountName.AddItem cell.Value
                Case 300 To 399
                    'and so on...
                Case 400 To 499
                    'and so on...
                Case Else
                    'and so on...
            End Select
        Next cell
    End With
    
End Sub


Assuming your "Chart of Accounts" worksheet tab has the code name: Sht_CoA (you can adjust that as you see fit)

I also included something (a little crude, but maybe OK for you) that will look at cell B3 and expand the range downward so you can have a variable sized chart of accounts range reference; the line of code is: Set CoA = Sht_CoA.Range(cell, cell.End(xlDown))
(where cell is temporarily assigned to "B3", although I prefer to use defined workbook names for such references in code)

On a side note, I hope you're associating this code with the userform initialize and option button change events (as opposed to a standard code module or the worksheet code module.) In which case you could just ignore or substitute "Me" in place of the references to: frmAddAccount

Also, it would seem a little odd to just have account numbers appear in a single column listbox as opposed to the account number with some sort of a name or description (at least a 2 column listbox).
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,702
Members
452,938
Latest member
babeneker

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