Slow vba

charly1

Board Regular
Joined
Jul 18, 2023
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi all

I am in dire need of help here.

Below is the code that i have written into a user form textbox change event. it is horrendously slow. at least 6 seconds every time I type a letter into the textbox. The event populates a list box. That part of the code is fine. It then uses x lookup to populate an additional 4 list boxes and that is the part that needs fixing. I would be immensely grateful, if any of you pros out there can advise on how to restructure the code, so that it should work quickly.

VBA Code:
Private Sub TextBox1_Change()

 
 
   Dim Ary As Variant, Rws As Variant
   Dim TBVal As String
   Dim Ws As Worksheet

  
  
   Set Ws = Sheets("îôúç")
   TBVal = TextBox1.Value
   With Ws.ListObjects("Table1").DataBodyRange
      If TBVal = "" Then
         Ary = Ws.Evaluate("choosecols(" & .Address & " ,1 ,5)")
      Else
         TBVal = Replace(TBVal, Chr(34), Chr(34) & Chr(34))
         Rws = Filter(Ws.Evaluate(Replace("transpose(if(isnumber(search(""" & TBVal & """,@)),row(@)-min(row(@))+1,""X""))", "@", .Columns(5).Address)), "X", False)
         If UBound(Rws) < 0 Then
            Me.ListBox1.List = Array("No matches")
            Exit Sub
         ElseIf UBound(Rws) = 0 Then
            ReDim Preserve Rws(1)
         End If
         Ary = Application.Index(.Value, Application.Transpose(Rws), Array(1, 5))
      End If
   End With
   

   
   With Me.ListBox1
      .ColumnCount = UBound(Ary, 2)
      .List = Ary
   End With
 


 ListBox1.ColumnWidths = "0;"
 
 ScrollBar1.Value = ScrollBar1.Min
 ScrollBar1.Max = ListBox1.ListCount - 13



Dim Bb As Integer
Bb = ListBox1.ListCount
Dim ij As Integer
For ij = 1 To Bb

ListBox5.AddItem (ij)

Next ij



'here begins the problomatic part
'here begins the problomatic part
'here begins the problomatic part
'here begins the problomatic part


Dim searchrange As Range
Set searchrange = Worksheets("îôúç").ListObjects("Table1").ListColumns(1).DataBodyRange

Dim tableData As Range


Dim Rr As Long
Dim BbJ As Long

BbJ = (ListBox1.ListCount - 1)
Dim c As String

ListBox2.Clear
For Rr = 0 To BbJ
With ListBox2
.ColumnCount = 2
.ColumnWidths = "0;"
.AddItem
.List(Rr, 0) = ListBox1.List(Rr, 0)
Set tableData = Worksheets("îôúç").ListObjects("Table1").ListColumns(7).DataBodyRange
c = WorksheetFunction.XLookup(ListBox1.List(Rr, 0), searchrange, tableData)
.List(Rr, 1) = c
End With
Next


ListBox3.Clear
For Rr = 0 To BbJ
With ListBox3
.ColumnCount = 2
.ColumnWidths = "0;"
.AddItem
.List(Rr, 0) = ListBox1.List(Rr, 0)
Set tableData = Worksheets("îôúç").ListObjects("Table1").ListColumns(14).DataBodyRange
c = WorksheetFunction.XLookup(ListBox1.List(Rr, 0), searchrange, tableData)
.List(Rr, 1) = c
End With
Next

ListBox4.Clear
For Rr = 0 To BbJ
With ListBox4
.ColumnCount = 2
.ColumnWidths = "0;"
.AddItem
.List(Rr, 0) = ListBox1.List(Rr, 0)
Set tableData = Worksheets("îôúç").ListObjects("Table1").ListColumns(20).DataBodyRange
c = WorksheetFunction.XLookup(ListBox1.List(Rr, 0), searchrange, tableData)
.List(Rr, 1) = c
End With
Next
End Sub

Please help me!

Thank you so much!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Please post your workbook to a cloud website for download. No personal data ! Include a sampling of example data to run a few scenarios.
Provide download link here.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

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