• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

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

duytoi

New Member
Here is my entire VBA code. I have very limited knowledge of Excel and VBA, so I would greatly appreciate your help.
PHP:
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
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.

list3.png

This is my source data table, I would greatly appreciate any assistance
list4.png
list1.png
 
You can only use the built-in column headers if you output the data to a range somewhere and then use the rowsource property of the listbox, assigning the data range address (i.e. excluding the header row). The listbox will automatically use the row above for the headers.
 
You can only use the built-in column headers if you output the data to a range somewhere and then use the rowsource property of the listbox, assigning the data range address (i.e. excluding the header row). The listbox will automatically use the row above for the headers.
Thank you all for taking the time for me. The issue has been resolved. I used the function to copy all the titles from the source data as search titles. This method may not be optimal, but at least it helps me solve the problem for now.

Below is the entire code. If there is a better way, please share and help.
PHP:
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("A3:D20006").Value
    ReDim result(1 To UBound(arr, 1), 1 To 4)
    
    ' Copy headers to the result array
    result(1, 1) = arr(1, 1)
    result(1, 2) = arr(1, 2)
    result(1, 3) = arr(1, 3)
    result(1, 4) = arr(1, 4)
    
    a = 1 ' Start from 1 because 1st row is header
    
    For i = 2 To UBound(arr, 1) ' Start from 2 to skip headers in arr
        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.Clear
    lstwebsite.ColumnCount = 4
    lstwebsite.List = result
End Sub


Private Sub lstwebsite_Click()

End Sub

Private Sub UserForm_Initialize()
    Dim arr(), result(), i As Long
    
    arr = Sheets("Sheet1").Range("A3:D20006").Value
    ReDim result(1 To UBound(arr, 1), 1 To 4)
    
    ' Copy headers to the result array
    result(1, 1) = arr(1, 1)
    result(1, 2) = arr(1, 2)
    result(1, 3) = arr(1, 3)
    result(1, 4) = arr(1, 4)
    
    For i = 2 To UBound(arr, 1) ' Start from 2 to skip headers in arr
        result(i, 1) = arr(i, 1)
        result(i, 2) = arr(i, 2)
        result(i, 3) = arr(i, 3)
        result(i, 4) = arr(i, 4)
    Next i
    
    lstwebsite.ColumnCount = 4
    lstwebsite.List = result
End Sub
 
Back
Top