How to add a title to the search results in Excel 2016 using VBA

duytoi

New member
Local time
Today, 23:48
Joined
Jul 23, 2024
Messages
4
I have built a data table and a data search function for Excel using a VBA ListBox. Everything works perfectly, but now I want to add a title to the search results. Does anyone know how to do this?

Currently, the title is not included in the search results when the program runs.

Code:
Private Sub input_search_Change()
    Dim arr(), result, i As Long, a As Long, dk As String
    dk = input_search.Text
    
    arr = Sheets("Sheet1").Range("A4:D20006").Value
    ReDim result(1 To UBound(arr, 1), 1 To 6)
    
    For i = 1 To UBound(arr, 1)
        If arr(i, 2) Like "*" & dk & "*" Or _
         arr(i, 4) Like "*" & dk & "*" Then
        a = a + 1
        result(a, 1) = arr(i, 1)
        result(a, 2) = arr(i, 2)
        result(a, 3) = arr(i, 3)
        result(a, 4) = arr(i, 4)
        End If
    Next i
    
    lstwebsite = ""
    lstwebsite.Clear
    lstwebsite.List = result

End Sub
Private Sub UserForm_Initialize()
lstwebsite.List = Sheets("Sheet1").Range("A4:D20006").Value
End Sub

list3.png


I want the title to be added as in the example below:
list2.png

list1.png
 
Last edited:
you can only have Title (column headings) of the listbox when you use Range as source of your listbox.
 
you can only have Title (column headings) of the listbox when you use Range as source of your listbox.
list4.png

This is my source data table, I would greatly appreciate any assistance
 
share your workbook, so others can see your code.
 
share your workbook, so others can see your code.
Code:
Private Sub input_search_Change()
    Dim arr(), result, i As Long, a As Long, dk As String
    dk = input_search.Text
   
    arr = Sheets("Sheet1").Range("A4:D20006").Value
    ReDim result(1 To UBound(arr, 1), 1 To 6)
   
    For i = 1 To UBound(arr, 1)
        If arr(i, 2) Like "*" & dk & "*" Or _
         arr(i, 4) Like "*" & dk & "*" Then
        a = a + 1
        result(a, 1) = arr(i, 1)
        result(a, 2) = arr(i, 2)
        result(a, 3) = arr(i, 3)
        result(a, 4) = arr(i, 4)
        End If
    Next i
   
    lstwebsite = ""
    lstwebsite.Clear
    lstwebsite.List = result

End Sub
Private Sub UserForm_Initialize()
lstwebsite.List = Sheets("Sheet1").Range("A4:D20006").Value
End Sub

Here is my entire VBA code. I have very limited knowledge of Excel and VBA, so I would greatly appreciate your help.
 
Why can't you just put the title into a cell? Make it bold etc?
 
Why don't you just manipulate a Label's Caption property? That would be a common way to label things on an excel userform
 

Users who are viewing this thread

Back
Top Bottom