Option Explicit
Private Sub CommandButton1_Click()
Dim sValue As String, i As Long, j As Long, k As Long, m As Long
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) = True Then
j = j + 1
End If
Next
If j = 0 Then
MsgBox "No item was selected."
Exit Sub
End If
Sheet2.Cells.ClearContents
Dim arr As Variant
arr = getArrayOfData()
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
sValue = Trim$(arr(i, j) & "")
If (Len(sValue) <> 0) And (IsNumeric(sValue) = False) Then
For m = 0 To UserForm1.ListBox1.ListCount - 1
If UserForm1.ListBox1.Selected(m) And UserForm1.ListBox1.List(m) = sValue Then
k = k + 1
Sheet2.Cells(k, 1) = sValue
Sheet2.Cells(k, 2) = arr(i, j + 1)
End If
Next
End If
Next
Next
Me.Hide
End Sub
Private Sub UserForm_Initialize()
Dim Itm As String
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
Dim arr As Variant
Dim i As Long, j As Long
arr = getArrayOfData()
For i = 1 To UBound(arr, 1) 'rows
For j = 1 To UBound(arr, 2) 'columns
Itm = Trim$(arr(i, j) & "")
If Len(Itm) <> 0 Then
If IsNumeric(Itm) = False Then
d(Itm) = 1
End If
End If
Next
Next
If d.Count <> 0 Then
Me.ListBox1.List = d.Keys
End If
Set d = Nothing
End Sub
Private Function getRS(Optional ByVal strFilter As String = "") As Object
Const adOpenKeyset As Long = 1
Const adLockOptimistic = 3
Dim xlXML As Object
Dim adoRecordset As Object
Dim rng As Range
Set rng = Sheet1.UsedRange
Set adoRecordset = CreateObject("ADODB.Recordset")
Set xlXML = CreateObject("MSXML2.DOMDocument")
xlXML.LoadXML rng.Value(xlRangeValueMSPersistXML)
adoRecordset.Open xlXML, CursorType:=adOpenKeyset, LockType:=adLockOptimistic
If Len(strFilter) Then
adoRecordset.Filter = strFilter
End If
Set getRS = adoRecordset
End Function
Public Function getArrayOfData() As Variant
getArrayOfData = Sheet1.UsedRange
End Function