如何基于空单元格在Excel中进行线性插值

时间:2019-01-09 16:13:15

标签: excel vba interpolation linear-interpolation

第一个宏运行预定义的检查,以查找数据集y和z中的间隙:我们有数据x,该数据是从1到7的顺序。 y和z值是具有间隙的值,相对于x(顺序号),这些间隙需要进行插值。

解决方案如下所示

例如:

x     y    z  
1     2    3    
2     4    6    
3     ?    8  ' interpolation of y  between x=2 and x=4    
4     8    ?  ' interpolation of z between x=3 and x= 5    
5     12   14

编辑版本2

Public Sub Interpol_Klicken()

Dim i As Long
Dim x1 As Double
Dim x2 As Double
Dim y1 As Double
Dim y2 As Double
Dim x As Double
Dim rngY As Range
Dim iCol  As Long
Dim iRow  As Long

Set rngY = Worksheets("Z").Range("A1:D10")

 iRow = rngY.Rows.Count
 iCol = rngY.Columns.Count


 For iCol = 2 To rngY.Columns.Count
 For iRow = 2 To rngY.Rows.Count

 If rngY.Cells(iRow, iCol).Value = "" Then

 y2 = rngY.Cells(iRow, iCol).Offset(RowOffset:=1).Value
 y1 = rngY.Cells(iRow, iCol).Offset(RowOffset:=-1).Value

 x2 = rngY.Cells(iRow, 1).Offset(RowOffset:=1).Value
 x1 = rngY.Cells(iRow, 1).Offset(RowOffset:=-1).Value
 x = rngY.Cells(iRow, 1).Value

 rngY.Cells(iRow, iCol) = y1 + (y2 - y1) * (x - x1) / (x2 - x1)

 End If
 Next iRow
 Next iCol


 End Sub

0 个答案:

没有答案