Slow VBA Code - Help

Snypa

New Member
Joined
Nov 1, 2013
Messages
45
Hello guys,

I have some event in my spreadsheet that were taking over 2 seconds to fire. After some diagnostic work I have found the VBA code that is causing the 2-3 second slow down (when I comment this out it is instant again).

The code allows my users to type a reference number into a box OR the name of the customer and will bring up the customers details based on either. I am doing this be checking if a string or number has been entered and running code depending on the answer. If a string is entered it checks the string on a table in another worksheet and changes the value of the cell to that customer's number.

Please can you help me and tell my why this code takes 2-3 seconds to run and how I could improve that?

Many thanks!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ' Check if value has changed in cell D4 (Store search)
    ' If it has, tries to find that customer in the customer list
    
    If Not Intersect(Target, Range("B5")) Is Nothing Then
        If Range("B5").Value <> vbNullString Then
            If IsNumeric(Range("B5").Value) Then
                Range("B5").Value = Range("B5").Value

            Else
                Dim strSearch As String
                Dim strOut As String
                Dim bFailed As Boolean

                strSearch = "*" & Range("B5") & "*"

                On Error Resume Next

                strOut = Application.WorksheetFunction.VLookup(strSearch, Worksheets("StoreLookup").Range("A2:B1173"), 2, False)

                If Err.Number <> 0 Then bFailed = True

                On Error GoTo 0

                    If Not bFailed Then

                        Range("B5").Value = strOut

                    Else

                        If Right(Range("B5").Value, 9) <> "not found" Then
                        Range("B5").Value = strSearch & " not found"

                    End If

                    End If
            End If
        End If
    End If
  
End Sub
 
Last edited:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Your function is implicitly recursive. It a Worksheet_Change() event but then you're changing the contents of the cells in the macro which will fire the macro again. You might consider using:

Code:
Application.EnableEvents = False

At the start of the function and turn them back on (=True) at the end.

WBD
 
Upvote 0
Your function is implicitly recursive. It a Worksheet_Change() event but then you're changing the contents of the cells in the macro which will fire the macro again. You might consider using:

Code:
Application.EnableEvents = False

At the start of the function and turn them back on (=True) at the end.

WBD

Legend! Thank you very much!!!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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