Home » excel » excel – How to make it fast the display result in Search in textbox MORE THAN 60000 rows

excel – How to make it fast the display result in Search in textbox MORE THAN 60000 rows

Posted by: admin May 14, 2020 Leave a comment


I have Excel Database which i just want to input only the tag number and i will automatic display the result in textbox… but in takes 30sec to display the result

Private Sub cmdSearch_Click()
Dim x As Long
Dim y As Long
x = Sheets("Clients").Range("A" & Rows.Count).End(xlUp).Row
For y = 1 To x
If Sheets("Clients").Cells(y, 1).Text = TextBox1.Value Then

TextBox1.Text = Sheets("Clients").Cells(y, 1)
TextBox4.Text = Sheets("Clients").Cells(y, 3)
TextBox5.Text = Sheets("Clients").Cells(y, 4)
TextBox10.Text = Sheets("Clients").Cells(y, 5)
TextBox11.Text = Sheets("Clients").Cells(y, 6)
TextBox12.Text = Sheets("Clients").Cells(y, 7)
TextBox13.Text = Sheets("Clients").Cells(y, 8)
End If
Next y
End Sub
How to&Answers:

Match is pretty fast

Private Sub cmdSearch_Click()
    Dim m As VARIANT

    With Sheets("Clients")
        m = Application.Match(TextBox1.Value, .Columns(1), 0)
        If not iserror(m) then
            TextBox4.Text = .Cells(m, 3)
            TextBox4.Text = .Cells(m, 4)
        end if
    end with

End Sub


Looping through 60,000 rows of data will be slow. Why don’t you try Range.Find() instead? The documentation is here https://docs.microsoft.com/en-us/office/vba/api/excel.range.find

A sample code from that page is

With Worksheets(1).Range("a1:a500") 
    Set c = .Find(2, lookin:=xlValues) 
    If Not c Is Nothing Then 
        firstAddress = c.Address 
            c.Value = 5 
            Set c = .FindNext(c) 
        Loop While Not c Is Nothing
    End If 
End With

In your code you could do something like the following. Declare a range variable and set it to the result of the find command. From the found range you can offset to the desired columns to retrieve their values.

dim result as range
x = Sheets("Clients").Range("A" & Rows.Count).End(xlUp).Row
set result = Sheets("Clients").Range("A1:A" & x).Find(TextBox1.Value, lookin:=xlValues)
if not result is nothing then
   TextBox1.Text = result.value
   TextBox4.Text = result.offset(0,2).value
' and so on. use offset to get results from other columns in the row where the found range is
end if