Log in

View Full Version : Show listbox items based on combobox selection, place listbox selections to bookmark



sadas
02-02-2015, 09:22 PM
I'm wanting to create a multi-column listbox for my userform, which displays a particular list of items based on a selection from a combobox.
Once the item/items have been selected from the listbox I then want the contents of one column placed at one bookmark then the contents of another placed at a different bookmark.
So the combobox contains names: Council1, Council2, Council 3, Council4. Then I want the listbox to display certain items based on which Council is selected. The items will have a few columns but I'm hoping to hide those, as each item column has different wording for that item (which will hopefully get placed at a particular bookmark).
Has anyone had a similar situation like this?
Thanks in anticipation

gmaxey
02-03-2015, 01:49 PM
Nobody wants to write your code from scratch. What have you tried?

You will need something like and initialize event to load the comboox. You will need a change event to detect changes in the combobox and you will need a click event to detect a selection in the listbox. Something like this:


Option Explicit
Private Sub ComboBox1_Change()
Dim lngIndex As Long
ListBox1.Clear
Select Case True
Case ComboBox1.Value = ComboBox1.List(0)
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = "A Miller"
.List(.ListCount - 1, 1) = "Male"
.AddItem
.List(.ListCount - 1, 0) = "J Jones"
.List(.ListCount - 1, 1) = "Female"
End With
Case ComboBox1.Value = ComboBox1.List(1)
Case ComboBox1.Value = ComboBox1.List(2)
Case ComboBox1.Value = ComboBox1.List(3)
Case Else
Beep
End Select

End Sub
Private Sub ListBox1_Click()
MsgBox ListBox1.Column(0) & " is a " & ListBox1.Column(1)
End Sub
Private Sub UserForm_Initialize()
Dim arrCB() As String
arrCB = Split("Council1,Council2,Council3,Council4", ",")
ComboBox1.List = arrCB
End Sub

sadas
02-03-2015, 04:47 PM
Sorry Greg I thought my code had posted, anyway below is what I have thus far.


Option Explicit
Private Sub ComboBox1_Change()
Dim lngIndex As Long
ListBox1.Clear
Select Case True
Case ComboBox1.Value = ComboBox1.List(0)
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = "Zone 1"
.List(.ListCount - 1, 1) = "Residential Area"
.AddItem
.List(.ListCount - 1, 0) = "Zone 2"
.List(.ListCount - 1, 1) = "Commerical Area"
End With
Case ComboBox1.Value = ComboBox1.List(1)
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = "Zone 1a"
.List(.ListCount - 1, 1) = "Farming Area"
.AddItem
.List(.ListCount - 1, 0) = "Zone 1b"
.List(.ListCount - 1, 1) = "Industrial Area"
End With
Case ComboBox1.Value = ComboBox1.List(2)
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = "Zone 1"
.List(.ListCount - 1, 1) = "North"
.AddItem
.List(.ListCount - 1, 0) = "Zone 2"
.List(.ListCount - 1, 1) = "South"
End With
Case ComboBox1.Value = ComboBox1.List(3)
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = "Zone 2a"
.List(.ListCount - 1, 1) = "East"
.AddItem
.List(.ListCount - 1, 0) = "Zone 2b"
.List(.ListCount - 1, 1) = "West"
End With
Case Else
Beep
End Select
End Sub
Private Sub UserForm_Initialize()
Dim arrCB() As String
arrCB = Split("Council1,Council2,Council3,Council4", ",")
ComboBox1.List = arrCB
End Sub
Private Sub CommandButton1_Click()
Dim BmkNm As String, NewTxt As String, BmkRng As Range
Dim i As Long
With Me.ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) = True Then Selection.InsertAfter .Column(0, i)
Next
End With
Application.ScreenRefresh
End Sub

The problem I'm having is getting the particular selection/s from the listbox inserted at the bookmark. I've highlighted where I think the problem is but can't seem to work it out. I'm hoping I can add column 1 for instance not column 0 to the bookmark

gmaxey
02-03-2015, 05:29 PM
What bookmarks? What is BmkNm, NewTxt, and BmkRng?


Private Sub CommandButton1_Click()
WriteToBM "Zone", ListBox1.Column(0)
WriteToBM "CompassDir", ListBox1.Column(1)
Unload Me
End Sub
Sub WriteToBM(strName As String, strValue As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks(strName).Range
oRng.Text = strValue
ActiveDocument.Bookmarks.Add strName, oRng
End Sub

sadas
02-04-2015, 08:06 PM
That code you supplied works a treat. I seem to have an issue when I change the Listbox to multiple selection, from single. I need this because their maybe multiple zones that a user needs to select. To make it slightly more difficult I would like to try get each selection in the Listbox write to different bookmark (there will always be one zone, but possibly 2 or 3). So the first selection will write the contents of a column to bookmark Zone1, the second selection (if made) will write to bookmark Zone2 etc.