Selected Answer
Halk
Your files has 31,799 lines so your code will run slowly when a single digit is entered into TextBox8 (you can expect a list box of 3,000 lines on average to be produced). Also you are looping through each character of the 31,799 cells in column A of sheet "archives"-
I suggest:
- you use the Instr function instead to see if TextBox8 contents are in the cell (it will return a number >0 if so)
- you do not try that until a few characters are already in TextBox8- I've assumed your invoice number is 4 characters so allow a search after 3 charaters are entered (so I only do something / clear the list only once there are 3 characters).
- (REVISION) load the invoice data from "Archives" into an array then run through that instead- it's must faster (a search with 3 charcters took less than a second on my fairly slow PC)
This revised code is in the attached, revised workbook. Changes tests are in bold (and now-unnecessary lines removed):
Option Base 1
Private Sub TextBox8_Change()
Dim sh As Worksheet, ArchiveArray As Variant
Set sh = Sheets("archives")
ArchiveArray = sh.Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).Value 'grab data into the array
Dim i As Long
If Len(TextBox8.Value) >= 3 Then 'if three characters entered...
Me.ListBox1.Clear
For i = 1 To UBound(ArchiveArray, 1) 'run through array
If InStr(LCase(ArchiveArray(i, 1)), Me.TextBox8) <> 0 Then ' add to list if chars match
With Me.ListBox1
.ADDITEM ArchiveArray(i, 1)
.List(ListBox1.ListCount - 1, 1) = ArchiveArray(i, 2)
.List(ListBox1.ListCount - 1, 2) = ArchiveArray(i, 3)
.List(ListBox1.ListCount - 1, 3) = ArchiveArray(i, 4)
.List(ListBox1.ListCount - 1, 4) = ArchiveArray(i, 5)
.List(ListBox1.ListCount - 1, 5) = ArchiveArray(i, 6)
.List(ListBox1.ListCount - 1, 6) = ArchiveArray(i, 7)
End With
End If
'Next x
Next i
End If
End Sub
(Note that things like
Application.ScreenUpdating don't add anything in this macro since you're not updating the worksheets).
Note that there was an error in Private Sub ListBox1_Click() which I corrected as follows:
Sheets("archives").Activate '### your worksheet was called "arcives" here
but I'm puzzled by that macro since controls
TextBox1 to
TextBox7 don't appear to exist.
On a minor note, I corrected the spelling to "cashier" in your sheets and UserForm.
If you really have nearly 32,000 invoice lines in the real world, you could get further speed improvements by using arrays.
Hope this helps.