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.
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