Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Mona Mehta   on Feb 10 In MS Office Category.

  
Question Answered By: Milton Robinson   on Feb 10

This is not difficult. Do the following:

1. Setup two columns of data in worksheet (sheet1):

- Enter "Category" in cell(A1).
- Enter "Products" in cell(B1).
- Enter the different products  starting from cell(B2) going
downwards, with the corresponding category for each product  starting
from cell(A2) downwards.

2. Create a new form  (frm1) with two listboxes (lstCategory &
lstProducts)

3. Paste the following lines of code in the code window of frm1:


'--------------------------------------------------------------------

Private Sub UserForm_Activate()
Dim rngCategory As Range, rngDummy As Range
Dim colCategories As New Collection
Dim element As Variant

' Identify the range of category cells
With ThisWorkbook.Worksheets("Sheet1")
Set rngCategory = .Range(.Range("A2"), .Range("A2").End
(xlDown))
End With

' Get a list of UNIQUE categories into a new collection
On Error Resume Next
For Each rngDummy In rngCategory
colCategories.Add rngDummy, CStr(rngDummy)
Next rngDummy
On Error GoTo 0

' Fill the lstCategories listbox with the UNIQUE categories
For Each element In colCategories
lstCategory.AddItem element
Next element

select  the first category in lstCategory listbox
lstCategory.ListIndex = 0
End Sub



Private Sub lstCategory_Change()
Dim strSelectedCategory As String
Dim rngCategory As Range, rngDummy As Range

' Get the name of the selected  category
strSelectedCategory = lstCategory.Column(0, lstCategory.ListIndex)

' Empty lstProducts listbox
lstProducts.Clear

' Identify the range of category cells
With ThisWorkbook.Worksheets("Sheet1")
Set rngCategory = .Range(.Range("A2"), .Range("A2").End
(xlDown))
End With

' Show products that match the selected category
For Each rngDummy In rngCategory
If rngDummy = strSelectedCategory Then
lstProducts.AddItem rngDummy.Offset(0, 1)
End If
Next rngDummy
End Sub

Share: 

 

This Question has 7 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Synchronize two combo boxes on a user form Or get search suggestion and latest updates.


Tagged: