简体   繁体   English

在一系列细胞中使用公式

[英]Using a formula in a range of cells

I want to use a formula in a range of cells but I get the formula in that cell and not the results of the formula. 我想在一系列单元格中使用公式,但是我在该单元格中得到公式,而不是公式的结果。

I tried a couple of different ways. 我尝试了几种不同的方法。

The first script I tried entered the formula all the way down the column until the last row value: - 我尝试的第一个脚本在列中一直输入公式,直到最后一行值为止:-

Dim LastRow As Long
LastRow = Cells(Rows.Count, 5).End(xlUp).Row
Range("DO22:DO" & LastRow).Formula = "IF=COUNTIF(AS22:AU" & LastRow & " ,""=Major Variance"")>0,""Major Variance"",IF(COUNTIF(AS22:AU" & LastRow & " ,""=Minor Variance"")>0,""Minor Variance"",""On Track""))"

This is the other script I tried but this puts the formula only in the first row of the range: - 这是我尝试过的另一个脚本,但这只是将公式放在范围的第一行: -

Dim LastRow As Long  
Dim Rng As Range
Range("DO22:DO" & LastRow).Select
For Each Rng In Range("DO22:DO" & LastRow)
    ActiveCell.Formula = "IF=COUNTIF(AS22:AU" & LastRow & " ,""=Major Variance"")>0,""Major Variance"",IF(COUNTIF(AS22:AU" & LastRow & " ,""=Minor Variance"")>0,""Minor Variance"",""On Track""))"
Next Rng

Edit 1 编辑1

I have made a change to the code as per someone's answer but I am now getting an run-time error 'Application-defined or object defined error. 我已经按照某人的答案对代码进行了更改,但是现在遇到了运行时错误“应用程序定义的错误或对象定义的错误”。

It only seems to happen when I add the extra = in front of the If. 只有当我在If前添加extra =时才会发生这种情况。 The error appears on the line 错误出现在行上

Rng.Formula = "=IF=COUNTIF(AS22:AU" & LastRow & " ,""=Major Variance"")>0,""Major Variance"",IF(COUNTIF(AS22:AU" & LastRow & " ,""=Minor Variance"")>0,""Minor Variance"",""On Track""))"

Here is the edited script: - 这是编辑后的脚本:-

Dim Rng As Range
Dim LastRow As Long

LastRow = Cells(Rows.Count, 5).End(xlUp).Row

Range("DO22:DO" & LastRow).Select
For Each Rng In Range("DO22:DO" & LastRow).Cells
    Rng.Formula = "=IF=COUNTIF(AS22:AU" & LastRow & " ,""=Major Variance"")>0,""Major Variance"",IF(COUNTIF(AS22:AU" & LastRow & " ,""=Minor Variance"")>0,""Minor Variance"",""On Track""))"
    Rng.Value = Rng.Value
Next Rng

You are using activecell in the 2nd one so it will only be in the active cell if not selected. 您在第二个单元格中使用了活动单元格,因此如果未选中它,它将仅在活动单元格中。 so you need to change your loop to be 所以你需要改变你的循环

 For Each Rng In Range("DO22:DO" & LastRow).cells

and then 接着

rng.Formula = "=IF=COUNTIF(AS22:AU" & LastRow & " ,""=Major Variance"")....

then 然后

rng.value=rng.value

also, in the 2nd one, youre not setting LastRow. 另外,在第二个中,您未设置LastRow。

You have a malformed IF function and COUNTIF function . 您有错误的IF函数COUNTIF函数

'target formula "=IF(COUNTIF(AS22:AU99, "Major Variance"), "Major Variance", =IF(COUNTIF(AS22:AU99, "Minor Variance"), "Minor Variance", "On Track"))
rng.Formula = "=IF(COUNTIF(AS22:AU" & lastRow & ", ""Major Variance""), ""Major Variance"", " & _
               "IF(COUNTIF(AS22:AU" & lastRow & ", ""Minor Variance""), ""Minor Variance"", " & _
               """On Track""))"
rng = rng.Value

Note that if both Major Variance and Minor Variance exist, then Major Variance takes precedence. 请注意,如果存在主要方差和次要方差,则主要方差优先。

To put this formula into each row and modify the row each time in a loop, the code would look like this. 要将此公式放入每一行并在循环中每次修改该行,代码将如下所示。

Dim lastRow As Long, rng As Range

With Worksheets("Sheet1")       '<~~ you should know what worksheet you are on!
    lastRow = .Cells(Rows.Count, 5).End(xlUp).Row
    For Each rng In .Range("DO22:DO" & lastRow)
        rng.Formula = "=IF(COUNTIF(AS" & rng.Row & ":AU" & rng.Row & ", ""Major Variance""), ""Major Variance"", " & _
                       "IF(COUNTIF(AS" & rng.Row & ":AU" & rng.Row & ", ""Minor Variance""), ""Minor Variance"", " & _
                       """On Track""))"
        rng.Value = rng.Value
    Next rng
End With

But you can also put the formula into all of the cells at once like this. 但你也可以像这样一次将公式放入所有细胞中。

Dim lastRow As Long

With Worksheets("Sheet1")       '<~~ you should know what worksheet you are on!
    lastRow = .Cells(Rows.Count, 5).End(xlUp).Row
    With .Range("DO22:DO" & lastRow)
        .Formula = "=IF(COUNTIF(AS22:AU22, ""Major Variance""), ""Major Variance"", " & _
                       "IF(COUNTIF(AS22:AU22, ""Minor Variance""), ""Minor Variance"", " & _
                       """On Track""))"
        .Value = .Value
    End With
End With

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

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