Match function slow to fail

PcMkr42

New Member
Joined
Jul 21, 2014
Messages
7
I have a code that works very quickly when it finds a value in a large spreadsheet. If it does not find the value it takes a couple seconds to register. Anyway I can speed it up?

This program has a list of values on one sheet and takes each value to see if it is in another sheet. If found it transfers the values from the data sheet to the orignal sheet. The highlighted code is what seems to be slowing it down.

Code:
Sub FindMO()
    Dim Count1 As Long
    Dim Count2 As Long
    Dim LastRow1 As Long
    Dim LastRow2 As Long
    Dim DataRange As Range
       Dim MONumber As Variant
    Dim RowNumber As Variant
       Dim bln As Boolean

'New Method

    screenUpdateState = Application.ScreenUpdating
    statusBarState = Application.DisplayStatusBar
    calcState = Application.Calculation
    eventsState = Application.EnableEvents

    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    
    LastRow1 = Worksheets("Sheet1").Columns("AE").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    LastRow2 = Worksheets("Sheet2").Columns("A").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    Set DataRange = ActiveWorkbook.Worksheets("Sheet2").Range("A1:A" & LastRow2)
    Count1 = 4
    Worksheets("Sheet1").Columns("L:L").Font.bold = False
    
    While Count1 <= LastRow1
        MONumber = Worksheets("Sheet1").Cells(Count1, 12).Value
        If MONumber = "" Then GoTo Line1
[B]        For Count2 = 1 To LastRow2
            RowNumber = Application.Match(MONumber, DataRange, 0)
            If Not IsError(RowNumber) Then
                bln = True
                Exit For
            End If
        Next Count2[/B]
        If bln = True Then
            'Transfer Data to Orignal Sheet
            Worksheets("Sheet1").Cells(Count1, 11).Value = Worksheets("Sheet2").Cells(RowNumber, 4).Value 
            Worksheets("Sheet1").Cells(Count1, 10).Value = Worksheets("Sheet2").Cells(RowNumber, 3).Value 
            Worksheets("Sheet1").Cells(Count1, 3).Value = Worksheets("Sheet2").Cells(RowNumber, 8).Value 
            Worksheets("Sheet1").Cells(Count1, 2).Value = Worksheets("Sheet2").Cells(RowNumber, 2).Value
            Else
            Worksheets("Sheet1").Cells(Count1, 12).Font.bold = True
        End If
Line1:
        bln = False
        Count1 = Count1 + 1
    Wend
    'Restore state
    Application.ScreenUpdating = screenUpdateState
    Application.DisplayStatusBar = statusBarState
    Application.Calculation = calcState
    Application.EnableEvents = eventsState
    
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I found the problem. I had the match searching in a loop that was not needed. So it would search many many extra times to get the same result. I commented out the code that cause the issues.

Code:
[B]        
        'For Count2 = 1 To LastRow2
            RowNumber = Application.Match(MONumber, DataRange, 0)
            If Not IsError(RowNumber) Then
                bln = True
                'Exit For
            End If
        'Next Count2
[/B]
 
Upvote 0

Forum statistics

Threads
1,217,364
Messages
6,136,117
Members
449,993
Latest member
Sphere2215

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