PDA

View Full Version : userform listbox query



mandeepmm1
06-30-2009, 05:07 AM
Hi friends,

I am developing a userform for a university timetble problem. i had a query related to it.

i have a list of say about 5 courses and each of them have their respective modules. i have these courses in a list box. there is a second list box for modules

what i need to know is that is there a way such that if the user selects a particular course then only the modules for that course come up in the second listbox and not the other ones ?

i would really appreciate if someone guides me about the procedure to do this. thanks in advance

GTO
06-30-2009, 05:58 AM
Greetings,

A very simple example, but you can use the ListIndex of the first ListBox to repopulate the second.

In a blank wb, new userform with two listbox(es) named by default...


Option Explicit

Dim lstTwoAry_1
Dim lstTwoAry_2

Private Sub ListBox1_Click()
With ListBox1
'// when listbox1 is clicked on, use the listindex to clear and
'// re-populate the modules shown in listbox2
If .ListIndex = 0 Then
ListBox2.Clear
ListBox2.List = lstTwoAry_1
ElseIf .ListIndex = 1 Then
ListBox2.Clear
ListBox2.List = lstTwoAry_2
End If
End With
End Sub

Private Sub UserForm_Initialize()
'// One array of 'modules' per course//
lstTwoAry_1 = Array("Big", "Tall")
lstTwoAry_2 = Array("Little", "Short")

'// Me is the userform in this case//
With Me
'// array of courses //
.ListBox1.List = Array("Choice One", "Choice Two")
'// Initialize the userform with the modules from course one showing...
.ListBox2.List = lstTwoAry_1
'// ...and show course one as highlighted/selected.//
.ListBox1.ListIndex = 0
End With
End Sub


Does that help at all?

Mark