Option Explicit
Function NS2GPEn(VungTra As Range, Mai As Double, TocDo As Double, Size As Double)
' Ham Noi Suy 2 Chieu Voi 3 ham So:'
Dim Rws As Long, Col As Byte: Const SoMai = 4
Dim x1 As Double, x2 As Double, y1 As Double, y2 As Double
Dim a11 As Double, a12 As Double, a21 As Double, a22 As Double
Dim t1 As Double, t2 As Double
Dim Rg0 As Range, sRng As Range, rMai As Range
Dim WF As Object, ClsX As Range, ClsY As Range
Rws = VungTra.Rows.Count: Col = VungTra.Columns.Count
Set WF = Application.WorksheetFunction: Set Rg0 = VungTra.Cells(1, 1)
NS2GPEn = WF.Lookup(Size, Rg0.Resize(Rws))
Set sRng = Rg0.Resize(Rws).Find(NS2GPEn, , xlFormulas, xlWhole)
If Not sRng Is Nothing Then
Set rMai = sRng.Offset(, 1).Resize(SoMai)
x1 = WF.Lookup(Mai, rMai)
Set ClsX = rMai.Find(x1): Rws = ClsX.Row
x2 = ClsX.Offset(1).Value
Else
NS2GPEn = "Chieu Rong Kenh": Exit Function
End If
y1 = WF.Lookup(TocDo, Rg0.Resize(, Col))
Set ClsY = Rg0.Resize(, Col).Find(y1)
y2 = ClsY.Offset(, 1).Value: Col = ClsY.Column
a11 = Cells(Rws, Col): a22 = Cells(Rws + 1, Col + 1)
a12 = Cells(Rws, Col + 1): a21 = Cells(Rws + 1, Col)
t1 = (a12 - a11) * (TocDo - y1) / (y2 - y1) + a11
t2 = (a22 - a21) * (TocDo - y1) / (y2 - y1) + a21
NS2GPEn = (t2 - t1) * (Mai - x1) / (x2 - x1) + t1
End Function