I have data in columns 1 and 2 of Sheet 1 (115 rows). These are my reference values. I have data in columns 3 and 4 in ActiveSheet (10000+). These are my raw data values.
The code I have below will find the closest raw data value to the reference value by taking the difference, finding the values between 0 and 15 minutes, finding the minumum value and copying other data from that row over to another location.
I use 2 auto filters in the process to do this for every reference value, so it happens 115 times. My questions are as follows:
- Are adding/remove the filters slowing down my search? Should I just look through all 10,000 data values rather than filtering for the same day first?
- When I find the value of the minimum value of a filtered list, is there a better way to quickly copy over data from other columns of the same row?
I have added a timer at the end of this code to help quantify. I hope you can help!!
Sub UpdatedTimeMatcherwithFilters()
'Make the code as fast as possible
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim MinValue, MinRow As Integer
Dim searchRange, Rng As Range
Dim elapsedTime As Integer
startTime = Time
Set searchRange = Range("G1:G1697")
'Count the reference values
TotalRefVal = Worksheets("Sheet1").Cells(Rows.Count, 5).End(xlUp).Row
'Count the Raw Data Values
Set Rng = Range("C2:C1000")
'TotalRawDataVal = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row
'Format the Columns as needed
Columns("G:G").Select
Selection.NumberFormat = "[h]:mm:ss;@"
Columns("I:I").Select
Selection.NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"
Columns("H:H").Select
Selection.NumberFormat = "m/d/yy;@"
For j = 2 To TotalRefVal
'Filter for date of reference value
ActiveSheet.Range("$C:$C").AutoFilter Field:=1, Operator:=xlFilterValues, _
Criteria2:=Array(2, "10/10/2014")
'Find the difference in times between Reference Data and Raw Data,
'put the difference value in the same row
For Each d In Rng.SpecialCells(xlCellTypeVisible)
ActiveSheet.Cells(d.Row, 7) = Worksheets("Sheet1").Cells(j, 5) - d
Next d
'Turn off filter in column C
ActiveSheet.AutoFilterMode = False
'Filter the Time Differences between 0 and 15 minutes
ActiveSheet.Range("$G:$G").AutoFilter Field:=1, Criteria1:=">0:00:00", _
Operator:=xlAnd, Criteria2:="<0:15:00"
'Find the Minimum value between 0 and 15 minutes
MinValue = Application.Min(searchRange.SpecialCells(xlCellTypeVisible))
'Find the Row of the value of the minimum difference
MinRow = Application.Match(MinValue, searchRange, 0)
'Copy the Data from columns C and D of minimum value's row
Cells(j, 9).Value = Cells(MinRow, 3)
Cells(j, 10).Value = Cells(MinRow, 4)
ActiveSheet.AutoFilterMode = False
Next j
stopTime = Time
elapsedTime = (stopTime - startTime) * 24 * 60 * 60
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Elapsed time, screen updating on: " & elapsedTime & _
" sec."
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub