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.
Please help me!
Thank you so much!
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!