You are here:

Excel/Enhancing the formula

Advertisement


Question
Hello Tom,
Remember, you wrote a custom formula to look up on the basis of both time range and B Number? Well its working fine. Could you please help me in enhancing the formula a bit including a time zone clause in this?
The formula you wrote was as follows:
Current formula = Findmatch(A1,B1,TableTime,TableB)
Anticipated formula= Findmatch(A1,B1,TableTime,TableB,GMT)
Where GMT is a value it could be +6 or -6. If i put 6, then it will consider by shifting the time to 6 hour.
For example
Table A:
Time    B #   Number of sec
2:45   1196025335   10

Table B:
Time    B #   Number of sec
8:46   1196025335   12

Previously, Findmatch(A1,B1,TableTime,TableB) would yield a result #N/A but now i want the formula to calculate that table is 6 hour lagging behind and now calculate if the time is withing 5 minute range.

Thank you for your great help as always.
Here is the previous formula that you wrote to me.

Option Explicit
Sub ShowValues()
Dim r As Range, cell As Range, res As Variant
Set r = Range("A3", Range("A3").End(xlDown))
For Each cell In r
 If cell.Row Mod 100 = 0 Then Debug.Print cell.Row
 res = FindMatch1(cell, cell.Offset(0, 1), Range("Time_Small"), Range("B_Small"))
 If Not IsError(res) Then
    cell.Offset(0, 4).Value = res
 Else
    cell.Offset(0, 4).Value = "#N/A"
 End If
Next

End Sub



Public Function FindMatch1(rTime As Range, rBnum As Range, rTime1 As Range, rBnum1 As Range)
Dim diff As Double, k As Long, i As Long, j As Long
Dim vt, vb, t, n, r As Range, s As String
vt = rTime1.Value
vb = rBnum1.Value
t = rTime
n = rBnum
j = -10
diff = CDbl(TimeValue("00:05:00"))
k = rBnum1.Row - 1
s = "Row: "
For i = LBound(vb, 1) To UBound(vb, 1)

If n = vb(i, 1) Then
  s = s & i + k & ", " & rTime1(i).Text & ", " & rBnum1(i).Offset(0, 1) & "| "
  Debug.Print s
  If Abs(vt(i, 1) - t) < diff Then
     j = i
     Exit For
  End If
End If
Next
If j >= 0 Then
 Set r = rBnum1(j).Offset(0, 1)
 FindMatch1 = r.Value
Else
  If Len(s) > 5 Then
    s = Left(s, Len(s) - 2)
    Debug.Print s
    FindMatch1 = s
  Else
    FindMatch1 = CVErr(xlErrNA)
  End If
End If
Debug.Print s
End Function




Option Explicit
Public Function FindMatch(rTime As Range, rBnum As Range, rTime1 As Range, rBnum1 As Range)
Dim diff As Double, k As Long, i As Long, j As Long
Dim vt, vb, t, n, r As Range
vt = rTime1.Value
vb = rBnum1.Value
t = rTime
n = rBnum
j = -10
diff = CDbl(TimeValue("00:05:00"))
k = rBnum1.Row - 1
For i = LBound(vb, 1) To UBound(vb, 1)

If n = vb(i, 1) Then
  If Abs(vt(i, 1) - t) < diff Then
     j = i
     Exit For
  End If
End If
Next
If j >= 0 Then
 Set r = rBnum1(j).Offset(0, 1)
 FindMatch = r.Value
Else
    FindMatch = CVErr(xlErrNA)
End If
End Function  

Answer
Hirok,

It isn't clear to which column the differential would be applied, but assume the right most column


Public Function FindMatch(rTime As Range, rBnum As Range, rTime1 As Range, rBnum1 As Range GMT as double)
Dim diff As Double, k As Long, i As Long, j As Long
Dim vt, vb, t, n, r As Range
vt = rTime1.Value
vb = rBnum1.Value
t = rTime
n = rBnum
j = -10
diff = CDbl(TimeValue("00:05:00"))
k = rBnum1.Row - 1
For i = LBound(vb, 1) To UBound(vb, 1)

If n = vb(i, 1) Then
 If Abs((vt(i, 1) + GMT) - t) < diff Then  '<== changed
    j = i
    Exit For
 End If
End If
Next
If j >= 0 Then
Set r = rBnum1(j).Offset(0, 1)
FindMatch = r.Value
Else
   FindMatch = CVErr(xlErrNA)
End If
End Function



If you want to apply it to the leftmost value (as single number then
you would not change that line.  You would change


t = rTime

to

t = rTime + GMT

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2012 About.com, a part of The New York Times Company. All rights reserved.