简体   繁体   English

Excel VBA-将字符串公式转换为一系列单元格中的Excel计算

[英]Excel VBA - Convert a string Formula to an Excel Calculation in a range of cells

I'm just wondering if this is possible to do without a loop - In my excel sheet, in, say, Range("A1:C10") I have text concatenation formulas that, once concatenated, create real Excel functions. 我只是想知道是否可以不进行循环-在我的excel工作表中,例如Range("A1:C10")我有文本连接公式,一旦连接,就可以创建真正的Excel函数。

As a stupid example, suppose I had the following in cell A1: 作为一个愚蠢的示例,假设我在单元格A1中具有以下内容:

A1: ="=Sum(D"&C2&":E"&C3&")"

Now, I know in VBA I can do something along the following for any one specific cell: 现在,我知道在VBA中,可以对任何一个特定的单元格执行以下操作:

Range("A1").Formula = Range("A1").Text

And it will convert my text formula into an Excel formula and evaluate it. 它将把我的文本公式转换为Excel公式并进行评估。

Now, what I'm curious about is, whether there a way to say, for example: 现在,我很好奇的是,是否有一种说法,例如:

Range("A1:C10").Formula = Range("A1:C10").Text

Without looping through each cell individually? 是否没有单独遍历每个单元?

Also, I can't use INDIRECT() as, unfortunately, my formulas refer to closed workbooks :/ 另外,我不能使用INDIRECT()因为不幸的是,我的公式引用的是封闭的工作簿:/

Any ideas?? 有任何想法吗??

Range.Text contains the string representation of the cell's value. Range.Text包含单元格值的字符串表示形式。 The actual calculated value (which I suspect is what you're after) is accessed using Range.Value - try this: 实际的计算值(我怀疑这是您想要的)是使用Range.Value访问的-尝试以下操作:

Range("A1:C10").Formula = Range("A1:C10").Value

Not sure if this is what you are trying to do, but if for example you use: 不知道这是否是您要尝试的操作,但是例如,如果您使用:

Range("A1:C10").Formula = "=Sum(D1:E1)"

then the relative references will be auto adjusted: 然后相对参考会自动调整:

A1: =Sum(D1:E1)
A2: =Sum(D2:E2)
B1: =Sum(E1:F1)
... etc.

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

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