【问题标题】:Insert COUNTIF formula when you have variable holding the value当您有变量保存值时插入 COUNTIF 公式
【发布时间】:2011-08-24 07:58:34
【问题描述】:

考虑:

 Cells(2, "Q").Formula = "=COUNTIF(P$1:P1,P2)=0"

当我有一个包含值的变量时,如何插入这些公式?

我必须从 3550 行和 4000 行开始计算公式。这取决于数据。好吧,当我用谷歌搜索它时,我什么也没找到。他们都使用相同的公式,但我需要在特定单元格中插入 countif 函数,可能是 300 或 500 - 这取决于变量值。

 Cells(count,"Q").formula = "=COUNTIF(cells($1,"P"):cells(count-1,"P"),cells(count,"P))=0"

是这样吗?好吧,我在某些方面进行了尝试,但最终以红色突出显示了这条线。如何使用变量插入这些公式?

【问题讨论】:

    标签: vba excel excel-2003 excel-formula


    【解决方案1】:

    试试这个:

    'case 1: if you know the destination range
    Range("Q2").Formula = "=COUNTIF(P$1:P1,P2)=0"
    Range("Q2").Copy Destination:=range("Q3:Q500")
    
    'case 2: if the destination range is a variable
    'minRow is a Long >= 1
    Range("Q" & minRow + 1).Formula = "=COUNTIF(P$" & minRow & ":P" & minRow & ",P" & minRow + 1 & ")=0"
    Range("Q" & minRow + 1).Copy Destination:=Range("Q" & minRow + 1 & ":Q" & maxRow)
    

    参考:Issun's answer Stack 溢出问题How do I insert the formula into the cell when the formula keeps changing with increase in row?

    【讨论】:

    • 。嘿,它不会从 Q3 开始,可能是从 Q300 我的意思是,你已经给出了结束复制的行尾,但没有给出开始行,我的意思是从哪里开始。起始行不会总是 3 可能是 300 somtimes。请更新你的答案
    • @niko:这是一个例子,你可以自己调整它。无论如何,我已经编辑了我的答案
    • 但是 countif 呢问题是关于 countif 而不仅仅是目的地!
    • @Issun 感谢您帮助我回答问题,我收到了关于 excel vba 的最后一个问题,所以 Issun 准备好回答我关于 excel VBA 的最后一个问题,如果这些问题得到解决,那么我的项目就完成了!感谢上帝,请回答我的问题。
    猜你喜欢
    • 2017-05-28
    • 2019-01-20
    • 2017-07-23
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    相关资源
    最近更新 更多