如何引用该函数被调用的单元格

情况

我想创build一个用户定义的函数,它将在用stringreplacestring中的variables之后,解决指定单元格中的string公式。

信息总是在两列。 第一列的顶部将包含以stringforms存储的公式。 第一列的底部将包含UDF。 公式和UDF之间将是string公式中的所有variables。 第二列将包含variables的所有数值。

问题

我不知道如何在自动方法中select信息列的顶部或UDF上方的variables,因为我不知道如何定义UDF所在的单元格的位置。

目的

引用UDF所在单元格的位置,以便可以定义公式单元格的位置和variables单元格的范围。 我正在试图以一种不需要将公式单元格或variables的地址传递给UDF的方式来编写UDF。 我希望它能够根据与UDF直接相邻的所有信息自行获取这些信息,而且没有任何差距。

我拥有的

Option Explicit Public Function SolvedEquation() As Long Dim FormulaCell As Range Dim Equation As String Dim VariableRange As Range Dim VariableCell As Range Dim VariablesLength As Integer Dim Variable As String Dim VariableValue As Double 'define FormulaCell as the last nonblank up from the cell the function is called in from a contiguous range(no spaces) FormulaCell = Application.ThisCell.End(xlUp).Select 'define the VariableRange as one up from the cell the function is called to second last cell non blank cell located upward in a contiguous selection (no spaces) VariableRange = Range(Cells(Application.ThisCell.Row - 1, Application.ThisCell.Column), Cells(FormulaCell.Row + 1, FormulaCell.Column)) Equation = FormulaCell.Value For Each VariableCell In VariableRange.Cells VariablesLength = Len(VariableCell.Value)-1 Variable = Left(VariableCell.Value, VariablesLength) VariableValue = Cells(VariableCell.Row, VariableCell.Column + 1).Value Equation = Replace(FormulaCell.Value, Variable, VariableValue) Next VariableCell SolvedEquation = Evaluate(Equation) End Function 

更好的编码build议赞赏(即select范围覆盖细胞)

数据的例子

在这里输入图像说明

在下面的一些代码更正之后,我终于解决了这个问题。 单元格中出现177.00,应该是176.86。 更正:通过重新定义variablestypes来解决最后一个问题,正如Chris在他的反馈中所做的那样。

您的代码中有许多与ThisCell没有直接关系的问题

请参阅行内评论

 Public Function SolvedEquation() As Variant '~~> allow for Error result Dim FormulaCell As Range Dim Equation As String Dim VariableRange As Range Dim VariableCell As Range Dim VariablesLength As Integer Dim Variable As String Dim VariableValue As Double 'define FormulaCell as the last nonblank up from the cell the function is called in from a contiguous range(no spaces) '~~> You must use Set and not use .Select '~~> but this wont give you what you want if the cell above ThisCell is blank 'Set FormulaCell = Application.ThisCell.End(xlUp) '~~> use this instead If Application.ThisCell.Row <= 2 Then ' Function is in row 1 or 2. What now? SolvedEquation = CVErr(xlErrNA) Exit Function Else If IsEmpty(Application.ThisCell.Offset(-1, 0)) Then Set FormulaCell = Application.ThisCell Else Set FormulaCell = Application.ThisCell.End(xlUp) End If End If 'define the VariableRange as one up from the cell the function is called to second last cell non blank cell located upward in a contiguous selection (no spaces) '~~> use Set '~~> define worksheet '~~> simplify 'VariableRange = Range(Cells(Application.ThisCell.Row - 1, Application.ThisCell.Column), Cells(FormulaCell.Row + 1, FormulaCell.Column)) With Application.ThisCell Set VariableRange = Range(.Offset(-1, 0), FormulaCell.Offset(1, 0)) End With Equation = FormulaCell.Value For Each VariableCell In VariableRange.Cells VariablesLength = Len(VariableCell.Value) '- 1 Variable = Left$(VariableCell.Value, VariablesLength) '~~> string version of Left is faster VariableValue = VariableCell.Offset(0, 1).Value '~~> simplify Equation = Replace$(Equation, Variable, VariableValue) '~~> string version of Replace is faster, continue to work on Equation Next VariableCell SolvedEquation = Evaluate(Equation) End Function 

也就是说,你的方法有一个固有的问题,当它的input数据改变时,它不会自动重新计算,因为在函数调用中没有对源数据的引用。 一个更好的方法是将Rangeparameter passing给引用公式和源数据的UDF,像这样

 Public Function SolvedEquation2(rng As Range) As Variant Dim dat As Variant Dim Equation As Variant Dim i As Long ' copy range data to an array dat = rng.Value ' Verify size of range If UBound(dat, 1) < 2 Or UBound(dat, 2) < 2 Then SolvedEquation2 = CVErr(xlErrNA) Exit Function End If ' Solve equation Equation = dat(1, 1) For i = 2 To UBound(dat, 1) Equation = Replace$(Equation, dat(i, 1), dat(i, 2)) Next ' Use Worksheet version of Evaluate SolvedEquation2 = rng.Worksheet.Evaluate(Equation) End Function 

注意:我不明白你为什么需要操作这些variables,所以我把它抛出去了。 如果需要这样做,用一些样本数据和预期的公式string更新你的Q,我将更新A

基于你的样本,公式将是SolvedEquation2(O129:P133)

注意:最好使用Worksheet版本的Evaluate。 查看查尔斯威廉姆斯的网站链接,这是为什么