Excel VBA loop slow

shakaib26

New Member
Joined
Feb 3, 2023
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

i have 80000 rows in "c"column with date in it. i am running below mentioned code to add the combobox list items on combobox change basis. When i run the macro it takes too long time on each change.

Can anybody suggest what to do to speed up the code.


Private Sub ComboBox2_Change()
Dim ws As Worksheet
Set ws = Worksheets("word by word")
Dim Lastrow, r, i As Long

For i = ComboBox2.ListCount - 1 To 0 Step -1
ComboBox2.RemoveItem i
Next i

Lastrow = ws.Range("c" & Rows.Count).End(xlUp).Row
For r = 3 To Lastrow
If InStr(ws.Cells(r, 3), ComboBox2.Text) > 0 Then
ComboBox2.AddItem (ws.Range("c" & r))

'code for showing list unique values only
Dim vArr()
ReDim vArr(1 To ComboBox2.ListCount)
vArr = ComboBox2.List
Dim e
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each e In vArr
If Not .exists(e) Then .Add e, Nothing
Next
If .Count Then ComboBox2.List = (.keys)
End With

End If
If ComboBox2.Text = "" Then
ComboBox2.Clear
End If
Next r
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Can you give us some examples?
Hi
Thanks for your reply

Like there are 3 parts in a name
Name: Khawaja Muhammad Shakaib
I need it should show results only on starting characters like khaw, muh, shak ie.......
But not shows results of mid characters like waja, mmad, aib ie......
Hopefully you will have understand what i want.
 
Upvote 0
Hi
Thanks for your reply

Like there are 3 parts in a name
Name: Khawaja Muhammad Shakaib
I need it should show results only on starting characters like khaw, muh, shak ie.......
But not shows results of mid characters like waja, mmad, aib ie......
Hopefully you will have understand what i want.
Like i start my search from waja, mmad, aib so, it should not show the results.
 
Upvote 0
Hi
Thanks for your reply

Like there are 3 parts in a name
Name: Khawaja Muhammad Shakaib
I need it should show results only on starting characters like khaw, muh, shak ie.......
But not shows results of mid characters like waja, mmad, aib ie......
Hopefully you will have understand what i want.

Do all names have 3 parts?
 
Upvote 0
If you type "mu khaw" do you expect it to match "Khawaja Muhammad Shakaib"? so the order of the keywords is irrelevant?
 
Upvote 0
If you type "mu khaw" do you expect it to match "Khawaja Muhammad Shakaib"? so the order of the keywords is irrelevant?
It should match only by typing "khaw"
If i search on muh ie.... Then it will also bring irrelevent words or names which last or mid characters are matching
Like there are two names "Khawaja Muhammad Shakaib" and other is "Muhammad Aslam"
I need names with khawaja if i search with keyword "muh" then it will bring khawaja but also bring "Muhammad Aslam" which i don't need and it will also increase the combobox list volume.
 
Upvote 0
So the keyword order matters, right?
Keyword "khaw mu" should match "Khawaja Muhammad Shakaib" but keyword "mu khaw" shouldn't.
Keyword "khaw" should match "Khawaja Muhammad Shakaib" but not "Shakaib Khawaja"
Try replacing "Sub get_filterX(ary)" with this:
VBA Code:
Sub get_filterX(ary)
'search without keyword order, case insensitive
'Like there are 3 parts in a name
'Name: Khawaja Muhammad Shakaib
'I need it should show results only on starting characters like khaw, muh, shak ie.......
'But not shows results of mid characters like waja, mmad, aib ie......

Dim i As Long, x, z, q, sv
Dim v As String
Dim flag As Boolean
    
    d.RemoveAll
    z = Split(UCase(ComboBox1.Value), " ")

    For Each x In ary
        flag = True: v = UCase(x)
        sv = Split(v, " ")
            If InStr(1, v, z(0), vbBinaryCompare) = 1 Then
                For i = 1 To UBound(z)
                    If i > UBound(sv) Then
                        flag = False: Exit For
                    Else
                         If InStr(1, sv(i), z(i), vbBinaryCompare) <> 1 Then flag = False: Exit For
                    End If
                Next
            Else
                flag = False
            End If
            
        If flag = True Then d(x) = Empty
    Next

End Sub
 
Upvote 1
T
So the keyword order matters, right?
Keyword "khaw mu" should match "Khawaja Muhammad Shakaib" but keyword "mu khaw" shouldn't.
Keyword "khaw" should match "Khawaja Muhammad Shakaib" but not "Shakaib Khawaja"
Try replacing "Sub get_filterX(ary)" with this:
VBA Code:
Sub get_filterX(ary)
'search without keyword order, case insensitive
'Like there are 3 parts in a name
'Name: Khawaja Muhammad Shakaib
'I need it should show results only on starting characters like khaw, muh, shak ie.......
'But not shows results of mid characters like waja, mmad, aib ie......

Dim i As Long, x, z, q, sv
Dim v As String
Dim flag As Boolean
   
    d.RemoveAll
    z = Split(UCase(ComboBox1.Value), " ")

    For Each x In ary
        flag = True: v = UCase(x)
        sv = Split(v, " ")
            If InStr(1, v, z(0), vbBinaryCompare) = 1 Then
                For i = 1 To UBound(z)
                    If i > UBound(sv) Then
                        flag = False: Exit For
                    Else
                         If InStr(1, sv(i), z(i), vbBinaryCompare) <> 1 Then flag = False: Exit For
                    End If
                Next
            Else
                flag = False
            End If
           
        If flag = True Then d(x) = Empty
    Next

End Sub
Thanks

Now it's perfect as per my requirement. Great work.
But only one problem in combobox list its showing only name which is in the start but it is now showing which is in 2nd or 3rd part of name, but when i populate the list data to listbox there is shows all name even 1st, 2nd or 3rd.
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    108.4 KB · Views: 10
Upvote 0

Forum statistics

Threads
1,216,793
Messages
6,132,723
Members
449,755
Latest member
TBertot107

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top