热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

目标寻求宏与目标作为一个公式-GoalSeekMacrowithGoalasaFormula

Imgraspingatstrawshere,soanyhelpwouldbegreat(ie.IhavenoideawhatImdoingwithVBA)

I'm grasping at straws here, so any help would be great (ie. I have no idea what I'm doing with VBA).

我在这里抓住稻草,所以任何帮助都会很棒(也就是说,我不知道我在用VBA做什么)。

I'm trying to solve a circular reference problem in Excel by creating a goal seek macro - basically

我试图通过创建目标搜索宏来解决Excel中的循环引用问题 - 基本上

  1. S3 = M + S2,
  2. S3 = M + S2,

  3. S3 = L * G.
  4. S3 = L * G.

I want to goal seek S3 to equal L * G by changing M.

我想通过改变目标来寻求S3等于L * G.

So, I've put

所以,我已经把

  • L * G in cell H32,
  • 细胞H32中的L * G,

  • S3 in H18,
  • 在H18的S3,

  • M in G18,
  • G18中的M,

  • S2 in F18
  • 在F18中的S2

and this is what I've gathered so far.

这是我迄今为止收集的内容。

Sub GoalSeek()   
Dim x As Integer
x = Range("H32").Value    
Range("H18").GoalSeek Goal:=x, ChangingCell:=Range("G18")     
End Sub

I'm getting a "Reference is not valid" error, ideas? Thanks!

我有一个“参考无效”的错误,想法?谢谢!

2 个解决方案

#1


4  

GoalSeek will throw an "Invalid Reference" error if the GoalSeek cell contains a value rather than a formula or if the ChangingCell contains a formula instead of a value or nothing.

如果GoalSeek单元格包含值而不是公式,或者如果ChangingCell包含公式而不是值或什么都不包含,则GoalSeek将抛出“无效引用”错误。

The GoalSeek cell must contain a formula that refers directly or indirectly to the ChangingCell; if the formula doesn't refer to the ChangingCell in some way, GoalSeek either may not converge to an answer or may produce a nonsensical answer.

GoalSeek单元格必须包含直接或间接引用ChangingCell的公式;如果公式没有以某种方式引用ChangingCell,则GoalSeek可能无法收敛到答案或者可能产生无意义的答案。

I tested your code with a different GoalSeek formula than yours (I wasn't quite clear whether some of the terms referred to cells or values).

我使用与您不同的GoalSeek公式测试了您的代码(我不太清楚某些术语是指单元格还是值)。

For the test, I set:

为了测试,我设置:

  the GoalSeek cell  H18 = (G18^3)+(3*G18^2)+6
  the Goal cell      H32 =  11
  the ChangingCell   G18 =  0 

The code was:

代码是:

Sub GSeek()
    With Worksheets("Sheet1")
        .Range("H18").GoalSeek _
        Goal:=.Range("H32").Value, _
        ChangingCell:=.Range("G18")
    End With
End Sub

And the code produced the (correct) answer of 1.1038, the value of G18 at which the formula in H18 produces the value of 11, the goal I was seeking.

并且代码产生了1.1038的(正确)答案,即G18的值,其中H18中的公式产生值11,这是我寻求的目标。

#2


1  

I think your issue is that Range("H18") doesn't contain a formula. Also, you could make your code more efficient by eliminating x. Instead, change your code to

我认为您的问题是Range(“H18”)不包含公式。此外,您可以通过消除x来提高代码的效率。相反,将代码更改为

Range("H18").GoalSeek Goal:=Range("H32").Value, ChangingCell:=Range("G18")

推荐阅读
author-avatar
dmcm0006
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有