简体   繁体   English

我在Excel / Mac中的VBA功能“该名称无效”

[英]“That name is not valid” for my VBA function in Excel/Mac

A simple VBA function. 一个简单的VBA功能。 When I try to use it in my worksheet, all I get, no matter what I do, is "That name is not valid". 当我尝试在工作表中使用它时,无论做什么,我得到的都是“该名称无效”。 I'm out of ideas. 我没主意了。

Sub FindABV(temperature)
With Worksheets("Sheet1")
    .Range("C28").GoalSeek _
    Goal:=temperature, _
    ChangingCell:=.Range("B28")
End With
FindABV = .Range("B28").Value
End Sub

I've tried creating a new Module to put it in. No change. 我尝试创建一个新模块来放入它。没有变化。 And no error indications from the code editor. 并且没有来自代码编辑器的错误指示。

The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code. Sub过程执行一个任务,然后将控制权返回给调用代码,但是它没有将值返回给调用代码。

See more here . 在这里查看更多。

This said, you cannot set a procedure equal to something: 也就是说,您不能将过程设置为等于以下内容:

FindABV = .Range("B28").Value

because that name is not valid (you cannot say that a procedure is equal to a certain value, it doesn't make sense). 因为该名称无效 (您不能说一个过程等于某个值,所以没有意义)。 You probably wanted to use a Function to return the value of that cell calculated by the Goal Seeker depending on the input temperature that you pass by the function: 您可能想使用一个函数来返回由目标搜索者计算出的该单元格的值,具体取决于您通过该函数传递的输入temperature

Function FindABV(temperature)
With Worksheets("Sheet1")
    .Range("C28").GoalSeek _
    Goal:=temperature, _
    ChangingCell:=.Range("B28")
End With
FindABV = .Range("B28").Value '<-- return the value
End Function

However, be careful: if =FindABV(temperature) lies on Sheet1.Range("B28") , you will have a circular reference because your function will try to have the value of itself. 但是,请注意:如果=FindABV(temperature)位于Sheet1.Range("B28") ,则将具有循环引用,因为函数将尝试具有其自身的值。

Your code will not deliver the results you want. 您的代码不会提供您想要的结果。 If you want to have the Function work for different values than the ones stored in B28 and C28 you'll have to write it more like this: 如果您要让Function处理与B28C28存储的值不同的值,则必须这样编写:

Public Function FindABV(goalCell As Range, changeCell As Range, temperature As Double)

    goalCell.GoalSeek Goal:=temperature, ChangingCell:=changeCell

    FindABV = changeCell
End Function

But this doesn't matter in any case because GoalSeek actually changes the value in the ChangingCell which Excel will not do if it is called from within a Function . 但是这并不能在任何情况下不要紧,因为GoalSeek实际上改变了价值ChangingCell其中Excel ,如果它是从内部调用不会做Function

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

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