I am currently using the following code to fill the list box lbSamDesc with the data in Column C on sheet EquipmentData where the adjacent B Column is blank. It then removes duplicates and alphabetizes the results.
Private Sub FillList()
Dim wsEquipment As Worksheet
Dim lLastRow As Long
Dim n As Long
Dim lIndex As Long
Dim vDataIn
Dim colData As Collection
Dim vDataOut()
Set wsEquipment = Worksheets("EquipmentData")
Me.lbSamDesc.Clear
With wsEquipment
lLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
If lLastRow > 3 Then
lIndex = 1
ReDim vDataOut(1 To lLastRow - 2)
Set colData = New Collection
vDataIn = .Range("B3:C" & lLastRow).Value
On Error Resume Next
For n = 1 To UBound(vDataIn, 1)
If Len(vDataIn(n, 1)) = 0 Then
colData.Add vDataIn(n, 2), CStr(vDataIn(n, 2))
If Err.Number = 0 Then
vDataOut(lIndex) = vDataIn(n, 2)
lIndex = lIndex + 1
Else
Err.Clear
End If
End If
Next n
If lIndex > 1 Then
ReDim Preserve vDataOut(1 To lIndex - 1)
'Me.lbSamDesc.List = vDataOut
Me.lbSamDesc.List = BubbleSort(vDataOut)
End If
Else
If .Cells(3, "B").Value = vbNullString Then Me.lbSamDesc.AddItem .Cells(3, "C").Value
End If
End With
End Sub
Public Function BubbleSort(Strings) As Variant
Dim a As Long
Dim e As Integer, f As Integer, g As Integer
Dim i As String, j As String
Dim m() As String, n() As Variant
e = 1
n = Strings
Do While e <> -1
For a = LBound(Strings) To UBound(Strings) - 1
i = n(a)
j = n(a + 1)
f = StrComp(i, j)
If f <= 0 Then
n(a) = i
n(a + 1) = j
Else
n(a) = j
n(a + 1) = i
g = 1
End If
Next a
If g = 1 Then
e = 1
Else
e = -1
End If
g = 0
Loop
BubbleSort = n
End Function
I am trying to do something similar where rather than using the criteria of instances where adjacent Column B is blank, but rather it directly searches the contents of Column C with the text in text box 'ItemDescription'.
I will assign this code to a command button, so when the user clicks it will display all cases where the text in ItemDescription finds a match in Column C (doesn't have to be an exact match, it could just match one of, or part of a word, or string of letters in a cell). The results should remove the duplicates, and then be ranked so the most likely item is at the top (so basically cells with the most amount of matching words or string of letters appear first).
Thanks,
James
Bookmarks