1

Hi every one I've written a code in Excel VBA it works , the code searches a worksheet and list the result in a list box specifying which columns to list but it takes very long time in responding to any change in search so I need to know the bug here is the code

Private Sub TbSearch_Change()
    Dim i As Long
    Dim MySheet As Worksheet
    With MySheet
        On Error Resume Next
        Set edRng = .Range("A4", .Range("A4").End(xlDown))
    End With
    'Set MySheet = Main
    'On Error Resume Next
    'Me.TbSearch.Text = StrConv(Me.TbSearch.Text, vbProperCase)

    Me.LBResult.Clear

    If CBRoads <> "" Then
        For i = 4 To Application.WorksheetFunction.CountA(edRng)
            For x = 1 To 5
                a = Len(Me.TbSearch.Text)
                If Left(MySheet.Cells(i, x).Value, a) = Me.TbSearch.Text And Me.TbSearch.Text <> "" Then

                    Me.LBResult.AddItem MySheet.Cells(i, 2).Value

                    For Each edCell In Me.LBResult.List
      
                        Me.LBResult.List(LBResult.ListCount - 1, 1) = MySheet.Cells(i, 3).Value
                        Me.LBResult.List(LBResult.ListCount - 1, 2) = MySheet.Cells(i, 8).Value
                        Me.LBResult.List(LBResult.ListCount - 1, 3) = MySheet.Cells(i, 9).Value
                        Me.LBResult.List(LBResult.ListCount - 1, 4) = MySheet.Cells(i, 10).Value
                        Me.LBResult.List(LBResult.ListCount - 1, 4) = MySheet.Cells(i, 12).Value
                    Next edCell

                End If
            Next x
        Next i
    Else
        MsgBox "choose first"
        Me.CBRoads.SetFocus
    End If
End Sub
1
  • Perhaps better posted on the Code review Stack since you say it works. Commented Mar 9, 2022 at 10:17

1 Answer 1

1

There is no reason to iterate over each item in Me.LBResult.List. It is killing your performance.

 For Each edCell In Me.LBResult.List
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.