下面是我的简单死亡率表混合的代码。死亡率表总是一个包含106个值的数组。运行此代码会导致#value错误,我无法弄清楚原因。
Public Function MorBlend(Table1 As Variant, Blend As Double, Table2 As Variant) As Variant
Dim Table3 As Variant, int1 As Double
int1 = 0
Do While int1 <= 105
Table3(int1) = Round(Table1(int1) * Blend / 1000000 + Table2(int1) * (1 - Blend) / 1000000, 6) * 1000000
int1 = int1 + 1
Loop
MorBlend = Table3
End Function
答案 0 :(得分:0)
作为UDF,这应该写成如下(或类似的东西):
Public Function MorBlend(Table1 As Variant, Blend As Double, Table2 As Variant) As Variant
'Table3 needs to be a two dimensional array, with the first dimension
'representing rows in the result, and the second dimension representing
'columns in the result
Dim Table3(1 To 106, 1 To 1) As Variant
Dim Age As Integer
For Age = 0 To 105
Table3(Age + 1, 1) = Round(Table1(Age + 1) * Blend / 1000000 + Table2(Age + 1) * (1 - Blend) / 1000000, 6) * 1000000
Next
MorBlend = Table3
End Function
然后可以在单元格C1:C106(例如)中使用此函数并作为数组公式输入(按 Ctrl - Shift - Enter 输入公式)
=MorBlend(A1:A106,.25,B1:B106)
注意:如果您想要允许从0岁到105岁的死亡(或其他)表,您需要修改代码以使用UBound
输入数组。上面的代码是硬编码的,可以精确地预期106个速率,并且不包含错误检查,以防止传入的内容不是。
如果您不打算将该函数用作UDF,则某些数组维度可能限制性较小 - 即您可以将数组的维度从0开始而不是1,并且您赢了“t = t需要有一个二维数组。
Public Function MorBlend(Table1 As Variant, Blend As Double, Table2 As Variant) As Variant
Dim Table3() As Variant
ReDim Table3(LBound(Table1) To UBound(Table1)) As Variant
Dim Age As Integer
For Age = LBound(Table1) To UBound(Table1)
Table3(Age) = Round(Table1(Age) * Blend / 1000000 + Table2(Age) * (1 - Blend) / 1000000, 6) * 1000000
Next
MorBlend = Table3
End Function
此版本的代码可用于以下程序:
Sub Main
Range("D2:D107") = Application.Transpose(Mor("71 GAM (50M/50F)"))
End Sub
Public Function Mor(Table As String) As Variant
Dim Tbl_71GAM_M50F50 As Variant
Dim Tbl_71GAM_M As Variant
Dim Tbl_71GAM_F As Variant
'Load tables from Excel ranges (e.g. "Tbl_716AM_M" may be the name of
' B2:B107, and "Tbl_716AM_F" may be the name of C2:C107)
Tbl_71GAM_M = Application.Transpose(Range("Tbl_716AM_M"))
Tbl_71GAM_F = Application.Transpose(Range("Tbl_716AM_F"))
'Create a merged table
Tbl_71GAM_M50F50 = MorBlend(Tbl_71GAM_M, 0.5, Tbl_71GAM_F)
If Table = "71 GAM Male" Then Mor = Tbl_71GAM_M
If Table = "71 GAM Female" Then Mor = Tbl_71GAM_F
If Table = "71 GAM (50M/50F)" Then Mor = Tbl_71GAM_M50F50
End Function
或者,如果要将Mor
作为数组UDF调用,可以使用以下
Public Function Mor(Table As String) As Variant
Dim Tbl_71GAM_M50F50 As Variant
Dim Tbl_71GAM_M As Variant
Dim Tbl_71GAM_F As Variant
'Load tables from Excel ranges (e.g. "Tbl_716AM_M" may be the name of
' B2:B107, and "Tbl_716AM_F" may be the name of C2:C107)
Tbl_71GAM_M = Application.Transpose(Range("Tbl_716AM_M"))
Tbl_71GAM_F = Application.Transpose(Range("Tbl_716AM_F"))
'Create a merged table
Tbl_71GAM_M50F50 = MorBlend(Tbl_71GAM_M, 0.5, Tbl_71GAM_F)
If Table = "71 GAM Male" Then Mor = Application.Transpose(Tbl_71GAM_M)
If Table = "71 GAM Female" Then Mor = Application.Transpose(Tbl_71GAM_F)
If Table = "71 GAM (50M/50F)" Then Mor = Application.Transpose(Tbl_71GAM_M50F50)
End Function
并输入一个数组公式,例如`= Mor(&#34; 71 GAM(50M / 50F)&#34;),例如,单元格D2:D107。
(这么多的可能性,所以显示它们的空间很小。)