VBA函数在不应该返回相同的结果时?

时间:2013-04-17 05:11:51

标签: function vba row

我编写了一个不会重新计算的函数interpolate_part_size,除非我单击Excel单元格并按Enter键,然后输出正确的结果。该函数的目的是插入一个值,将活动单元格的当前行与参考行进行比较。

我尝试了Application.Volatile但是它在同一个参数的每一行返回相同的结果,这是不正确的。使用F9重新计算也不起作用。

有什么建议吗?

Function interpolate_part_size(pp) As Double
'--- Subroutine to interpolate size

Dim part_size_high As Double
Dim part_size_low As Double
Dim pp_high As Double
Dim pp_low As Double
Dim low_index As Integer
Dim high_index As Integer
Dim current_row As Integer

With Application.WorksheetFunction

current_row = ActiveCell.Row
' match on the range of cells in the current row

high_index = .Match(pp, Worksheets("DATA").Range(Cells(current_row, 4), Cells(current_row, 29)), -1)
low_index = high_index + 1

pp_high = .Index(Worksheets("DATA").Range(Cells(current_row, 4), Cells(current_row, 29)), 1, high_index)
pp_low = .Index(Worksheets("DATA").Range(Cells(current_row, 4), Cells(current_row, 29)), 1, low_index)
part_size_high = .Index(Worksheets("DATA").Range("PSD"), 1, high_index)
part_size_low = .Index(Worksheets("DATA").Range("PSD"), 1, low_index)

End With

If pp_high = pp_low Then
    interpolate_part_size = part_size_low
    Else:
    interpolate_part_size = (pp - pp_low) / (pp_high - pp_low) * (part_size_high - part_size_low) + part_size_low
End If

End Function

0 个答案:

没有答案