简体   繁体   English

输入参数的Excel 2010 VBA用户书面功能修改

[英]Excel 2010 VBA User Written Function Modification of Input parameter

I have a question about Excel 2010 VBA. 我对Excel 2010 VBA有疑问。 Done plenty of searching for a solution but the answers don't address my issue. 做了很多寻找解决方案的尝试,但是答案并没有解决我的问题。 Hoping that the stackoverflow community will be able to shed some light for me. 希望stackoverflow社区能够为我提供一些启示。

I have a user written function that is called from cells in a worksheet. 我有一个用户编写的函数,该函数从工作表的单元格中调用。 It is called with two parameters – both cells on the same sheet. 使用两个参数调用它–两个单元都在同一张纸上。

The aim of the function is to review the two parameters and to return a string as the result of the function depending upon their values. 该函数的目的是检查两个参数,并根据它们的值返回一个字符串作为函数的结果。

This far the functions works as expected. 至此,该功能可以按预期工作。

However, if the value of the first parameter has a certain value, I want to be able to change the value of the second parameter. 但是,如果第一个参数的值具有某个值,我希望能够更改第二个参数的值。 This is where I get stuck. 这就是我卡住的地方。 I cannot find any way to alter its value. 我找不到任何改变其价值的方法。

I have tried just setting the parameter to the desired value but that causes an error. 我尝试将参数设置为所需的值,但这会导致错误。 I thought I might be able to set up a pointer to the active sheet and hence the cell but I don't know what its reference is (as there will be many on the sheet). 我以为可以建立指向活动工作表以及单元格的指针,但是我不知道它的引用是什么(因为工作表上会有很多)。

To be honest, I am not sure that I can do what I need. 老实说,我不确定我能做我所需要的。 Any thoughts or comments on a way forward will be gratefully received. 如有任何想法或意见,我们将不胜感激。

With regards 带着敬意

Graham Jones 格雷厄姆·琼斯(Graham Jones)

As User defined functions(UDF) cannot change the state of the workbook/worksheet etc.You can use the worksheet change event. 由于用户定义函数(UDF)无法更改工作簿/工作表等的状态,因此可以使用工作表更改事件。

Copy this code to the worksheet where you are entering the function 将此代码复制到要输入功能的工作表中

    Private Sub Worksheet_Change(ByVal Target As Range) 
Application.EnableEvents=False
     if Target.Address = "$A$1" and target.count = 1 Then ' change in first parameter - first parameter
       if Target.Value ="something" then    Range("B1").Value ="change"
         '$A$1 is the location of fist paramenter
         'range("b1") is second parameter on assumption
      end if
   Application.EnableEvents=True
    End Sub 

As a general rule, you don't want your Excel functions to change their inputs. 通常,您不希望Excel函数更改其输入。 I have done it before, but it's considered very bad practice, though. 我以前做过,但是,这被认为是非常不好的做法。

The more acceptable way would be to make a function that calculates the second parameter - This would take 2 inputs: 更可接受的方法是制作一个计算第二个参数的函数 -这将需要2个输入:

  1. The first parameter 第一个参数
  2. The default value for the second parameter 第二个参数的默认值

Then you use that finction to calculate the second parameter in your worksheet (In other words, rather than have the cell which contains your second parameter just be a set value, it is now calculated via this function) and then your original function can work as always with the second parameter always being the expected value. 然后,您可以使用该函数来计算工作表中的第二个参数(换句话说,不是让包含第二个参数的单元格只是一个设置值,而是现在通过此函数进行计算),然后您的原始函数可以像总是第二个参数始终是期望值。

I hope this makes sense - If not, just let me know and I'll supply an example. 我希望这是有道理的-如果没有,请让我知道,我将举一个例子。

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

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