首页 文章

使用VBA在一个单元格中插入公式

提问于
浏览
0

我知道这个主题已经被问过,我试图复制如何在一个单元格中插入公式,但是,我的vba代码中出现了错误 .

enter image description here

这是我的代码:

ws.Range("C9").Formula = "=CountIf(wsRD.Range(C & Rows.count).End(xlUp).Row, ""Event"")"   'CountIf(wsRD.Range("C" & Rows.count).End(xlUp).Row, "Event") 'count(Search("Event", wsRD.Range("C" & Rows.count).End(xlUp).Row, 1))

我需要在ws.Range(“C9”)中插入一个公式,其中,它总结了wsRD.Range(“C”和Rows.count)中值为“Event”的单元格的数量.End( xlUp).Row . 我可以知道代码中的问题是什么吗?感谢您的帮助 .

谢谢 .

2 回答

  • 0

    我相信这可能是正确的答案

    ws.Select
    LRow = ws.Range("C" & Rows.Count).End(xlUp).Row
    
    Range("C9").FormulaLocal = "=COUNTIF(C10:C" & LRow & ";""Event"")"
    

    所以基本上,我使用FormulaLocal来编写公式的方式与我在Excel中编写公式的方式相同,然后,因为公式必须是一个大字符串,我将它分成2个字符串,将值放入LRow,并使用&&连接

  • 2

    你可以摆脱 LRow 变量,如果你愿意,可以将它放在你的等式中 .

    Dim LRow as Long
    LRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row
    
    ws.Range("C9").Formula = "=COUNTIF(C10:C" & LRow & ", ""Event"")"
    

相关问题