excel 在一个单元格中插入公式

3pmvbmvn  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(113)

我知道这个主题已经被问到了,我试着复制如何在一个单元格中插入公式,但是,我在我的VBA代码中得到了一个错误。


的数据
下面是我的代码:

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).End(xlUp). Row中值为“Event”的单元格的计数。我可以知道我的代码中有什么问题吗?感谢您的帮助。
谢谢

nxowjjhe

nxowjjhe1#

你可以去掉LRow变量,如果你愿意的话,把它放在你的方程中。

Dim LRow as Long
LRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row

ws.Range("C9").Formula = "=COUNTIF(C10:C" & LRow & ", ""Event"")"

字符串

pkwftd7m

pkwftd7m2#

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

ws.Select
LRow = ws.Range("C" & Rows.Count).End(xlUp).Row

Range("C9").FormulaLocal = "=COUNTIF(C10:C" & LRow & ";""Event"")"

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

u59ebvdq

u59ebvdq3#

如果任何人,像我一样,想把高级Excel公式放在引用ByCol等Table函数的单元格中,解决方案应该是使用.Formula2Local将这些公式“填充”到单元格中。

Sub Populate_Formulas_Click()
    Dim Formula As Range
    Dim i As Integer
    i = 2               ' Start in column B
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    For Each Formula In Range("B13:M13")                              ' Source of Formulatext, added as Value and prepended with a single quote
            Sheets("Report").Select
            Cells(56, i).Formula2Local = Mid(Formula.Value2, 1)       ' Copy formula, after the first single quote prefix it with "="
            i = i + 1
    Next Formula
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub

字符串
好运

相关问题