T
tgilmour
I have a userform based on the "Cascading ListBoxes" example from Greg
Maxey's site http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm.
It populates list boxes dependent on the value of the prior list box.
How do I extend this functionality beyond 3 list boxes? For example,
how would I add the color (List Box 4) and cost (list box 5) of the
selected model?
ListBox1 Data (Manufacturer)
Dell (row 1)
HewlettPackard (row 2)
ListBox 2 Data (Category)
Desktop ¶ Notebook (row 1)
Desktop ¶ Notebook (row 2)
ListBox 3 Data (Model)
Dimension E520 | Dimension E521¶ Inspiron 6400 | Inspiron 9400 (row 1)
Pavillion d4650 | Pavillion a1650¶ Pavillion dv9000 | Pavillion dv6000
(row 2)
ListBox 4 (color)
Blue | Silver | Red ¶ Black | Brown | Grey (row 1)
Orange | Green ¶ White | Yellow (row 2)
ListBox 5 (cost)
$999 ¶ $1199 (row 1)
$1000 ¶ $1200 (row 2)
Thanks in advance for your help.
Here is the code.
Option Explicit
Private Sub UserForm_Initialize()
Dim myArray() As Variant
Dim sourcedoc As Document
Dim i As Integer
Dim j As Integer
Dim myitem As Range
Dim m As Long
Dim n As Long
Application.ScreenUpdating = False
Set sourcedoc = Documents.Open(FileName:="M:\Data.doc",
Visible:=False)
i = sourcedoc.Tables(1).Rows.Count - 1
j = sourcedoc.Tables(1).Columns.Count
ListBox1.ColumnCount = j
'Hide columns 2 and 3
ListBox1.ColumnWidths = "75;0;0"
ReDim myArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
myArray(m, n) = myitem.Text
Next m
Next n
'Load data into ListBox1
ListBox1.List() = myArray
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub
Private Sub ListBox1_Change()
Dim myArray As Variant
myArray = Split(ListBox1.List(ListBox1.ListIndex, 1), Chr(13))
ListBox2.List = myArray
ListBox3.Clear
End Sub
Private Sub ListBox2_Change()
Dim myArray1 As Variant
Dim myArray2 As Variant
myArray1 = Split(ListBox1.List(ListBox1.ListIndex, 2), Chr(13))
myArray2 = Split(myArray1(ListBox2.ListIndex), "|")
ListBox3.List = myArray2
End Sub
Maxey's site http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm.
It populates list boxes dependent on the value of the prior list box.
How do I extend this functionality beyond 3 list boxes? For example,
how would I add the color (List Box 4) and cost (list box 5) of the
selected model?
ListBox1 Data (Manufacturer)
Dell (row 1)
HewlettPackard (row 2)
ListBox 2 Data (Category)
Desktop ¶ Notebook (row 1)
Desktop ¶ Notebook (row 2)
ListBox 3 Data (Model)
Dimension E520 | Dimension E521¶ Inspiron 6400 | Inspiron 9400 (row 1)
Pavillion d4650 | Pavillion a1650¶ Pavillion dv9000 | Pavillion dv6000
(row 2)
ListBox 4 (color)
Blue | Silver | Red ¶ Black | Brown | Grey (row 1)
Orange | Green ¶ White | Yellow (row 2)
ListBox 5 (cost)
$999 ¶ $1199 (row 1)
$1000 ¶ $1200 (row 2)
Thanks in advance for your help.
Here is the code.
Option Explicit
Private Sub UserForm_Initialize()
Dim myArray() As Variant
Dim sourcedoc As Document
Dim i As Integer
Dim j As Integer
Dim myitem As Range
Dim m As Long
Dim n As Long
Application.ScreenUpdating = False
Set sourcedoc = Documents.Open(FileName:="M:\Data.doc",
Visible:=False)
i = sourcedoc.Tables(1).Rows.Count - 1
j = sourcedoc.Tables(1).Columns.Count
ListBox1.ColumnCount = j
'Hide columns 2 and 3
ListBox1.ColumnWidths = "75;0;0"
ReDim myArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
myArray(m, n) = myitem.Text
Next m
Next n
'Load data into ListBox1
ListBox1.List() = myArray
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub
Private Sub ListBox1_Change()
Dim myArray As Variant
myArray = Split(ListBox1.List(ListBox1.ListIndex, 1), Chr(13))
ListBox2.List = myArray
ListBox3.Clear
End Sub
Private Sub ListBox2_Change()
Dim myArray1 As Variant
Dim myArray2 As Variant
myArray1 = Split(ListBox1.List(ListBox1.ListIndex, 2), Chr(13))
myArray2 = Split(myArray1(ListBox2.ListIndex), "|")
ListBox3.List = myArray2
End Sub