Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

the code is very slow and close the file

0

hell

 I  have  file   and  contains  the  codes   it  should  bring    the  data  when  search  in textbox  based on sheet ARCHIVES  and  based on COL contain  the  invoice   and  show  the  data  in  list  boxes    but  it  stuck  and  close  the  file   so  before  anybody   run  the  userform  and  the  codes  looks  inside   may  be  find  why  the  code  is  slow  and  close  the  file    

sorry  if  my  file  is  mess

Answer
Discuss

Answers

0
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:

  1. you use the Instr function instead to see if TextBox8 contents are in the cell (it will return a number >0 if so)
  2. 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).
  3. (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.

Discuss

Discussion

John 
thanks   so  much   for   your  effort 
I    admit   now it's  good    but  it   is  still slow   because  the   huge of  data 
yes  I really have nearly 32,000 invoice lines 
Halk (rep: 30) Jun 6, '21 at 3:49 pm
Halk. See my revised answer/code and file (using arrays) which I just corrected. It should be much faster. You might be able to reduce the minuimum number of characters to 2 and it will still be fast enough. I.e. change to:
If Len(TextBox8.Value) >= 2 Then 'if two characters entered...
but that willl depend on your PC/ user expectations. If so, you'd need to change Label16 in UserForm4 too.
John_Ru (rep: 6142) Jun 6, '21 at 4:18 pm
I no  know  what     say   you   it  still seems  slowly  when  write  whether      the  second or  third  digit   it  takes  almost from 2 to 3 sec    so   if  it's   the   best  way   to  make  fast      just  inform  me   to  select  the  answer  and  close  this  thread 
thanks  again
Halk (rep: 30) Jun 6, '21 at 4:28 pm
Halk. I'm surprised (my PC is a 2.8Mhz i8 processor I think but took less than a second). I just tried it on a netbook (with just a 2GB internal drive) and that took a couple of seconds. It's getting late here and I can't think of a faster way on a large file (unless you declare the array at the form level and preload it when the form is first initialised), sorry
John_Ru (rep: 6142) Jun 6, '21 at 4:45 pm
BTW please check the code in the file you used does have lines like .ADDITEM ArchiveArray(i, 1) and .List(ListBox1.ListCount - 1, 1) = ArchiveArray(i, 2) since I had to correct the revised file I first uploaded
John_Ru (rep: 6142) Jun 6, '21 at 4:50 pm
John 
thanks   for  your  help    I  can't   ignore   your  answering    my   file  was  doesn't  work  at  all      and  you  provide  me  the  solution 
thanks  again 
Halk (rep: 30) Jun 6, '21 at 4:51 pm
No problem Halk, glad it works now (despite being a little slow). Thank you for selecting my answer,
John_Ru (rep: 6142) Jun 6, '21 at 4:57 pm
You didn't explain how TextBox1 to TextBox7 are used, incidentally but no problem if you're happy with how that macro particular works.
John_Ru (rep: 6142) Jun 6, '21 at 5:00 pm
about textbox1 to 7  are  existed  in  back   listbox  just  move  listbox   and  you  will   see   them  and  sorry  about  it  
bad  arranging !
Halk (rep: 30) Jun 6, '21 at 5:09 pm
Thanks, I see now. and presume that so you just use them to store info about selected lines from the list. That has no effect on your key issue of speed following the TextBox8_Change event
John_Ru (rep: 6142) Jun 6, '21 at 5:26 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login