gpt4 book ai didi

function - 在 VBA 函数中使用命名单元格

转载 作者:行者123 更新时间:2023-12-02 21:36:45 26 4
gpt4 key购买 nike

我有一个工作表,其中为某些单元格定义了名称。这些单元格将在函数中使用,我将使用它们的名称来调用它们。

但是,当我使用 Excel 调用函数时,我得到 0 作为函数的返回值,就好像名称未链接或具有 0 值一样。

下面是我写的代码。 “Sum_Len_1”、“L_W_2”和“L_W_1”是我为源单元指定的名称。

Function min_w(depth)

If depth < Sum_Len_1 Then
min_w = L_W_1 * 0.868 * depth / 1000
Else
min_w = L_W_1 * 0.868 * Sum_Len_1 / 1000 + L_W_2 * 0.868 * (depth - Sum_Len_1) / 1000
End If

End Function

如何解决这个问题?

最佳答案

如果您只是编写 min_w = L_W_1 * 0.868 * height/1000 vba 会认为 L_W_1 它是变量(属于 value=0 的类型变体)。您必须像这样 Range("L_W_1").Value 来引用指定的单元格。

如果将其更改为:

,它应该可以工作
Function min_w(depth As Long)
If depth < Range("SUM_LEN_1").Value Then
min_w = Range("L_W_1").Value * 0.868 * depth / 1000
Else
min_w = Range("L_W_1").Value * 0.868 * Range("SUM_LEN_1").Value / 1000 + Range("L_W_2").Value * 0.868 * (depth - Range("SUM_LEN_1").Value) / 1000
End If
End Function

关于function - 在 VBA 函数中使用命名单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17232674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com