Hi, everyone.
I'm looking for assistance with a multiple-selection list box being utilized to populate a selected cell with the selected choices and display previously selected items should the cell be selected again.
Currently, I've been able to create the list box which is populated with a list of items from a designated table and is activated upon clicking within a cell. Once a series of items are selected, pressing the "OK" button copies the selections to the cell using a comma as a delimiter. When clicking within this same cell, however, the list box displays the list of items but does not register the previous selected items that currently populate the cell resulting in those items being duplicated when selected again. Ideally, I'd like to be able to update the cell by checking and unchecking selections from within the list box.
Here is the code used to activate the list box upon clicking within a cell:
List Box Form Code:
Any help would be greatly appreciated!
I'm looking for assistance with a multiple-selection list box being utilized to populate a selected cell with the selected choices and display previously selected items should the cell be selected again.
Currently, I've been able to create the list box which is populated with a list of items from a designated table and is activated upon clicking within a cell. Once a series of items are selected, pressing the "OK" button copies the selections to the cell using a comma as a delimiter. When clicking within this same cell, however, the list box displays the list of items but does not register the previous selected items that currently populate the cell resulting in those items being duplicated when selected again. Ideally, I'd like to be able to update the cell by checking and unchecking selections from within the list box.
Here is the code used to activate the list box upon clicking within a cell:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strList As String
On Error Resume Next
'temporarily turn off Events
Application.EnableEvents = False
'set a range with all DV cells on sheet
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
'if no DV cells, exit macro
If rngDV Is Nothing Then GoTo exitHandler
If Not Intersect(Target, rngDV) Is Nothing Then
'if active cell IS in DV range
'check if it's a List (DV type 3)
If Target.Validation.Type = 3 And Cells(1, Target.Column) <> "Workgroup" And Cells(1, Target.Column) <> "Discipline" And Cells(1, Target.Column) <> "Position" Then
'if list, get source list name
strList = Target.Validation.Formula1
strList = Right(strList, Len(strList) - 1)
'pass source list name to global variable
strDVList = strList
'open UserForm
frmDVList.Show
End If
End If
exitHandler:
'turn on Events
Application.EnableEvents = True
End Sub
List Box Form Code:
VBA Code:
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdOK_Click()
Dim strSelItems As String
Dim lCountList As Long
Dim strSep As String
Dim strAdd As String
Dim bDup As Boolean
On Error Resume Next
strSep = ", " 'separator for items in cell
With Me.lstDV
'go through all items in list
' numbering starts at zero
For lCountList = 0 To .ListCount - 1
'if item is selected, get item name
' strAdd variable is item name
' or empty string
If .Selected(lCountList) Then
strAdd = .List(lCountList)
Else
strAdd = ""
End If
'if no previous items,
' strSelItems =strAdd
If strSelItems = "" Then
strSelItems = strAdd
Else
'if prev items, add separator
' and latest item
If strAdd <> "" Then
strSelItems = strSelItems _
& strSep & strAdd
End If
End If
Next lCountList
End With
With ActiveCell
'if active cell is not empty, add separator
' and all items collected from ListBox
If .Value <> "" Then
.Value = ActiveCell.Value _
& strSep & strSelItems
Else
'if active cell empty, and all items
' collected from ListBox
.Value = strSelItems
End If
End With
Unload Me
End Sub
Private Sub lstDV_Click()
End Sub
Private Sub UserForm_Initialize()
Me.lstDV.RowSource = strDVList
End Sub
Any help would be greatly appreciated!