如何在 Excel 中为函数创建引用变量?
Posted
技术标签:
【中文标题】如何在 Excel 中为函数创建引用变量?【英文标题】:How can I make a reference variable for a function in Excel? 【发布时间】:2021-12-23 17:20:26 【问题描述】:试图使变量在函数中起作用。这是如何做: 我有一个所得税计算器。它工作完美。如果我要使用 SUMPRODUCT,我也可以获得正确的值(如果我使用累积表,甚至是 VLOOKUP,但现在是 nvm;只要知道我也尝试过 VLOOKUP)。
SUMPRODUCT 公式如下:
=SUMPRODUCT(A4:A10-A3:A9,E1-C4:C10,N(E1>C4:C10))
我希望能够将 E1 替换为“x”,这样我就可以创建一个公式,所以 =Tax(E1)
会给我与 SUMPRODUCT 公式相同的结果。
问题是我不知道如何使用“x”作为变量。
使用=LET(x, E1, SUMPRODUCT(A4:A10-A3:A9,x-C4:C10,N(x>C4:C10)))
确实给出了正确的值。从这里我想把它变成一个更简单的公式,我可以通过公式 =Tax(E1)
或 E1 以外的任何单元格将“E1”动态更改为任何内容。
Picture of table with SUMPRODUCT formula
--------- 编辑 --------- 我最终研究了 SUMPRODUCT 函数并真正了解它是如何工作的,然后在谷歌上搜索了一堆 VBA 的东西。最后只是制作了一个处理整个事情的 VBA 函数。这是以防万一有人需要它:
Dim Rate1 As Single
Dim Rate2 As Single
Dim Rate3 As Single
Dim Rate4 As Single
Dim Rate5 As Single
Dim Rate6 As Single
Dim Rate7 As Single
Dim Over1 As Single
Dim Over2 As Single
Dim Over3 As Single
Dim Over4 As Single
Dim Over5 As Single
Dim Over6 As Single
Dim Over7 As Single
Dim Amount1 As Single
Dim Amount2 As Single
Dim Amount3 As Single
Dim Amount4 As Single
Dim Amount5 As Single
Dim Amount6 As Single
Dim Amount7 As Single
Dim Tax1 As Single
Dim Tax2 As Single
Dim Tax3 As Single
Dim Tax4 As Single
Dim Tax5 As Single
Dim Tax6 As Single
Dim Tax7 As Single
Dim Final As Single
Function Tax(Value As Single)
Rate1 = 0.1
Rate2 = 0.02
Rate3 = 0.1
Rate4 = 0.02
Rate5 = 0.08
Rate6 = 0.03
Rate7 = 0.02
Over1 = 0
Over2 = 9951
Over3 = 40526
Over4 = 86376
Over5 = 164926
Over6 = 209426
Over7 = 523601
Amount1 = Value - Over1
If Amount1 > 0 Then
Tax1 = Amount1 * Rate1
Final = Tax1
Amount2 = Value - Over2
Else
Amount2 = 0
End If
If Amount2 > 0 Then
Tax2 = Amount2 * Rate2
Final = Final + Tax2
Amount3 = Value - Over3
Else
Amount3 = 0
End If
If Amount3 > 0 Then
Tax3 = Amount3 * Rate3
Final = Final + Tax3
Amount4 = Value - Over4
Else
Amount4 = 0
End If
If Amount4 > 0 Then
Tax4 = Amount4 * Rate4
Final = Final + Tax4
Amount5 = Value - Over5
Else
Amount5 = 0
End If
If Amount5 > 0 Then
Tax5 = Amount5 * Rate5
Final = Final + Tax5
Amount6 = Value - Over6
Else
Amount6 = 0
End If
If Amount6 > 0 Then
Tax6 = Amount6 * Rate6
Final = Final + Tax6
Amount7 = Value - Over7
Else
Amount7 = 0
End If
Tax = Value - Final
End Function
【问题讨论】:
只有当你有 LAMBDA 函数时你才能这样做。否则,您将需要一个 UDF。 您希望它始终引用 a4:a10 等并且只更改 E1?那么为什么不使用 =SUMPRODUCT($A$4:$A$10-$A$3:$A$9,E1-$C$4:$C$10,N(E1>$C$4:$C$10)) 并锁定所有其他内容到位了吗? 【参考方案1】:如果您有 Excel 365,您现在可以使用 Lambda General description Examples 来定义没有 VBA 的函数。原则上它与 Let 几乎相同:
=LAMBDA(x, SUMPRODUCT(Sheet1!A4:A10-Sheet1!A3:A9,x-Sheet1!C4:C10,N(x>Sheet1!C4:C10)))
但实际上,您需要将引用设为绝对引用,否则公式会根据您调用它的位置而改变:
=LAMBDA(x, SUMPRODUCT(Sheet1!$A$4:$A$10-Sheet1!$A$3:$A$9,x-Sheet1!$C$4:$C$10,N(x>Sheet1!$C$4:$C$10)))
这是您在名称管理器中定义它的方式:
然后你可以简单地放
=Tax(E1)
在任何单元格中。
请注意,您可能需要订阅Office Insider 程序才能访问此功能,正如@Jos Woolley 所述。
【讨论】:
您可能应该将“如果您有 Excel 365”修改为“如果您有 Excel 365 并且还订阅了 Office 预览体验计划” 您可能是对的,但不同的用户似乎对推出此功能有不同的体验。以上是关于如何在 Excel 中为函数创建引用变量?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Microsoft MakeCode 中为 micro:bit 创建全局变量?
如何在 Kotlin Android 中为数据类创建空构造函数