简体   繁体   English

当具有列表验证的单元格被更新(用户从列表中选择其他值)时,另一个单元格将自动更新

[英]When a cell with list validation gets updated (user selects different value from list), another cell automatically updates

I have a cell that has a list validation that a user can select from a list of Names. 我有一个具有列表验证的单元格,用户可以从“名称”列表中进行选择。 When the user selects a Name (from list validation in "C7"), I want a cell right beneath it in "C9" to automatically call a function that I have written in a class module called CalculateGrade(). 当用户选择一个名称(从“ C7”中的列表验证)时,我希望“ C9”中它下方的单元格自动调用我在称为CalculateGrade()的类模块中编写的函数。 How can I trigger a cell value change event from a user selecting a different name from the list validation to execute the function CalculateGrade() in a different cell? 如何从用户从列表验证中选择其他名称来触发单元格值更改事件,以在其他单元格中执行函数CalculateGrade()?

Your function isn't recalculating because it has no way of knowing that the cell that changed value (eg C7) will change its outcome. 您的函数没有重新计算,因为它无法知道更改值的单元格(例如C7)将更改其结果。 You should have written the function to have C7 passed into it as a parameter. 您应该已经编写了将C7作为参数传递给该函数的函数。 Once this function has C7 as a precedent , any change in C7 will trigger a recalculation of the function. 一旦此函数以C7为先例 ,C7中的任何更改都将触发对该函数的重新计算。

You've decided to keep the code for the CalculateGrade() function a secret but it probably goes something like this: 您已经决定将CalculateGrade()函数的代码保密,但是它可能类似于以下内容:

function CalculateGrade()
    dim tmp as double, str as string

    str = range("C7").value
    'do something here to get a grade from the name in C7
    CalculateGrade = tmp

end function

This is how it should be written: 应该这样写:

function CalculateGrade(rList as range)
    dim tmp as double, str as string

    str = rList.value
    'do something here to get a grade from the new name in C7 (aka rList)
    CalculateGrade = tmp

end function

This modified function is not called like =CalculateGrade() ; 修改后的函数不像=CalculateGrade()那样被调用; it is called like =CalculateGrade(C7) . 它称为=CalculateGrade(C7) With C7 as a precedent to the function, any change in C7 will recalculate the function and return a new value. 以C7作为该函数的先例,C7中的任何更改都将重新计算该函数并返回一个新值。

A UDF can take advantage of all the tools available to native worksheet functions The commands in the Formulas ► Formula Auditing group are powerful diagnostic tools but greatly underutilized. UDF可以利用本机工作表功能可用的所有工具。公式►公式审核组中的命令是功能强大的诊断工具,但未得到充分利用。 There is a good discussion on the auditing tools available in Find and correct errors in formulas . 查找和更正公式中的错误中对可用的审核工具进行了很好的讨论。 Determining precedents is in the Display the relationships between formulas and cells section. 确定先例在“ 显示公式与单元格之间的关系”部分中。

There is an alternative using the Application.Volatile Method where the function will recalculate whenever anything in the workbook changes but passing in the cell containing the list validation as a precedent provides much more functionality and does not lock you down to a single cell to use for list validation like hard-coding the cell address does. 还有一种使用Application.Volatile方法的替代方法 ,该方法将在工作簿中的任何内容发生变化时重新计算该函数,但是将包含列表验证的单元格作为先例传递给您将提供更多的功能,并且不会将您锁定在单个单元格上以供使用像硬编码单元格地址那样进行列表验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM