Arrays 更改数组中每个值的用户定义函数
下面是我的简单死亡率表代码。死亡率表始终是一个具有106个值的数组。运行这段代码会导致#值错误,我不知道为什么Arrays 更改数组中每个值的用户定义函数,arrays,excel,function,vba,Arrays,Excel,Function,Vba,下面是我的简单死亡率表代码。死亡率表始终是一个具有106个值的数组。运行这段代码会导致#值错误,我不知道为什么 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(Tabl
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
Public Function MorBlend(表1为变量,Blend为双精度,表2为变量)为变量
尺寸表3为变型,int1为双精度
int1=0
当int1作为一个UDF时,应该写如下(或类似的内容):
然后可以在单元格C1:C106(例如)中使用此函数,并作为数组公式输入(按Ctrl-Shift-Enter键输入公式),如下所示:
=MorBlend(A1:A106.25,B1:B106)
注意:如果您希望允许死亡率(或其他)表不从0岁到105岁,则需要修改代码以使用输入数组的UBound
。上面的代码是硬编码的,期望精确到106个速率,并且不包括错误检查,以防传入错误
如果您不打算将函数用作自定义项,则某些数组尺寸标注的限制性可能较小,即,您可以从0开始而不是从1开始对数组进行尺寸标注,并且不需要二维数组
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
并在例如单元格D2:D107中输入一个数组公式,如“=Mor”(“71 GAM(50M/50F)”)
(如此多的可能性,显示它们的空间如此之小。)了解一下表1
、混合
和表2
是什么可能会有所帮助。您是在尝试将数组返回到代码中,还是将其用作工作表函数?为了让表3(int1)=……
行正常工作,您需要将Table3
声明为一个数组,当前下限为0,上限至少为int1
将达到的任何值。但如果您打算将其作为一个UDF,则下限必须为1,因此您需要将int1
初始化为1
。如果Table1
和Table2
是工作表上的单列范围,则Table1(0)
可能是指包含文本的标题行,而不是第一行数字数据。此外,您需要调整表3的大小。谢谢您的建议。表1和表2是两个死亡率表,都有106个值,blend是在blend中使用的表1的百分比。谢谢!如何调用此函数
,并将结果分配给另一个函数中的数组?我得到了一个#值
errorWait-当你说“使用函数返回到另一个函数”时,我以为你是指另一个VBA函数。但这不会产生一个#值错误。如果您将at用作自定义项,即使它将进入另一个Excel函数(例如as=MIN(MorBlend(A1:A106.25,B1:B106),MorBlend(A1:A106.5,B1:B106))
),您仍需要使用我的第一个版本的代码。谢谢。我尝试使用以下函数:公共函数Mor(Table As String)作为Variant Dim Tbl_Mor作为Variant Dim Tbl_71GAM_M50F50作为Variant Tbl_71GAM_m50=MorBlend(Tbl_71GAM_m,0.5,Tbl_71GAM_F)如果Table=“71 GAM男性”,那么Tbl_Mor=Tbl_71GAM如果Table=“71 GAM女性”,那么Tbl_Mor=Tbl_71GAM=Tbl_71GAM如果Table=“5071 GAM(50M/F)”然后Tbl_Mor=Tbl_71GAM_M50F50 Mor=Tbl_Mor End函数,但您的建议对我帮助很大,谢谢。
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