在Excel VBA中定义计数公式,不起作用

在Excel VBA中定义计数公式,不起作用,excel,vba,dynamic,formula,countif,Excel,Vba,Dynamic,Formula,Countif,如果有人能帮忙,我真的很感激;我已经做了一段时间了 我只想在单元格中定义countif的公式,下面是代码: 范围(“E”和PLrowstart).Formula=“=CountIf($B$PLrowstart:$B$PLrowend”)和“,B2)” PLrowstart和PLrowend是我在行之前设置的整数变量。计数if的范围为范围(“B”&PLrowstart&“:B”&PLrowend)。我也尝试过其他方法,但都不管用 TIA.试试这个 Sub SetFormula() PLrows

如果有人能帮忙,我真的很感激;我已经做了一段时间了

我只想在单元格中定义countif的公式,下面是代码:

范围(“E”和PLrowstart).Formula=“=CountIf($B$PLrowstart:$B$PLrowend”)和“,B2)”

PLrowstart和PLrowend是我在行之前设置的整数变量。计数if的范围为范围(“B”&PLrowstart&“:B”&PLrowend)。我也尝试过其他方法,但都不管用

TIA.

试试这个

Sub SetFormula()


PLrowstart = 2
PLrowend = 4

Range("E" & PLrowstart).Formula = "=CountIf($B$" & PLrowstart & ":$B$" & PLrowend & ",B2)"


End Sub

手动输入时,您的公式正确吗?天哪,非常感谢!我能再问一个问题吗?我也尝试过:Range(“E”)和PLrowstart.Formula=“=CountIf(Range(Cells(“&PLrowstart&”,2),Cells(&PLrowstart&“,2)),Cells(2,2)),Cells(2,2))”在excel中,它可以在文本中正确显示公式,类似于=CountIf(Range(Cells(Cells(2,2),Cells(10,2),Cells(2,2)),但是,它不工作,你对此有什么线索吗?范围和单元格是VBA方法。因此,它们不会在单元格中作为公式工作。非常感谢!!需要“$”吗?
Sub SetFormula()


PLrowstart = 2
PLrowend = 4

Range("E" & PLrowstart).Formula = "=CountIf($B$" & PLrowstart & ":$B$" & PLrowend & ",B2)"


End Sub