Function TwoDimInterpol(x As Double, Y As Double, Matrix As Range, OrderX As Integer, OrderY As Integer) As Variant 'function to perform two-dimentional linear interpolation in elevation tables 'INPUT: '-x Latitude '-Y LONGITUDE '-OrderX use digit 0 if X values are organized in descending manner, 1 if X values are ascending '-OrderY use digit 0 if Y values are organized in descending manner, 1 if Y values are ascending 'Matrix: table organized in the following manner: ' Y1 Y2 Y3 Y4 Y5 ' X1 value value value value value ' X2 value value value value value ' X3 value value value value value ' X4 value value value value value 'Reading the dimensions of the matrix 'Number of rows m = Matrix.Rows.Count 'Number of columns N = Matrix.Columns.Count 'Checking is value X is within the Matrix ranges If OrderX = 1 Then If x < Matrix(2, 1) Or x > Matrix(m, 1) Then Err = 1 End If Else If x > Matrix(2, 1) Or x < Matrix(m, 1) Then Err = 1 End If End If 'Checking is value Y is within the Matrix ranges If OrderY = 1 Then If Y < Matrix(1, 2) Or Y > Matrix(1, N) Then Err = 1 End If Else If Y > Matrix(1, 2) Or Y < Matrix(1, N) Then Err = 1 End If End If 'If values are not in the matrix range return a message flg = False If Err = 1 Then TwoDimInterpol = "Value not in range" Else 'Searching through the table For i = 2 To m - 1 If OrderX = 0 Then If x <= Matrix(i, 1) And x >= Matrix(i + 1, 1) Then For j = 2 To N - 1 If OrderY = 0 Then If Y <= Matrix(1, j) And Y >= Matrix(1, j + 1) Then flg = True Exit For End If Else If Y >= Matrix(1, j) And Y <= Matrix(1, j + 1) Then flg = True Exit For End If End If Next End If Else If x >= Matrix(i, 1) And x <= Matrix(i + 1, 1) Then For j = 2 To N - 1 If OrderY = 0 Then If Y <= Matrix(1, j) And Y >= Matrix(1, j + 1) Then flg = True Exit For End If Else If Y >= Matrix(1, j) And Y <= Matrix(1, j + 1) Then flg = True Exit For End If End If Next End If End If If flg = True Then Exit For End If Next VAR_x11_i = lininterpol(Matrix(i, 1), Matrix(i, j), Matrix(i + 1, 1), Matrix(i + 1, j), x) Var_x12_i = lininterpol(Matrix(i, 1), Matrix(i, j + 1), Matrix(i + 1, 1), Matrix(i + 1, j + 1), x) TwoDimInterpol = lininterpol(Matrix(1, j), VAR_x11_i, Matrix(1, j + 1), Var_x12_i, Y) End If End Function Function lininterpol(x1, y1, x2, y2, x) lininterpol = (((y2 - y1) / (x2 - x1)) * (x - x1)) + y1 End Function