I'm building a form which involves populating list boxes, where the user can select from a list of values in box A (lstLocations) to transfer to list B (lstMainLocs).
I'm using the code below:
The data pasted into txtDiagrams is essentially CSV format. The function it refers to is this:
The main problem is when the function is called by the sub 'txtDiagrams_keyUp', it will add duplicate values to the listbox, but when the same function is called by the following sub, it works perfectly:
Any ideas why?
Thanks
Chris
I'm using the code below:
VBA Code:
Private Sub txtDiagrams_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'Split the pasted data into an array, one line for each diagram
arrDiagrams() = Split(txtDiagrams.Text, vbCrLf)
'Populate the list box with the available locations
With lstLocations
'Clear the list box ready for new data
.Clear
'Get the location for each diagram
For lngCounter = 0 To UBound(arrDiagrams()) - 1
arrLine = Split(arrDiagrams(lngCounter), vbTab)
strLocName = arrLine(1)
Call AddEntry("lstLocations", strLocName)
Next
'Select the first item
.ListIndex = 0
End With
End Sub
The data pasted into txtDiagrams is essentially CSV format. The function it refers to is this:
VBA Code:
Function AddEntry(lstName As String, strData As String)
Set lstBox = Me.Controls(lstName)
With lstBox
If .ListCount = 0 Then
.AddItem strData
ElseIf strData > .List(.ListCount - 1) Then
.AddItem strData
Else
For lngLocalCount = 0 To .ListCount - 1
If strData < .List(lngLocalCount) Then
.AddItem strData, lngLocalCount
Exit For
End If
Next
End If
End With
End Function
The main problem is when the function is called by the sub 'txtDiagrams_keyUp', it will add duplicate values to the listbox, but when the same function is called by the following sub, it works perfectly:
VBA Code:
Private Sub cmdAddMain_Click()
With lstLocations
For lngListCount = 0 To .ListCount - 1
If .Selected(lngListCount) = True Then
Call AddEntry("lstMainLocs", .List(lngListCount))
End If
Next
End With
lstLocations.ListIndex = -1
End Sub
Any ideas why?
Thanks
Chris