excel 在COUNTIF公式中使用变暗范围&单元格.行

8cdiaqws  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(152)

下午好。我有一个非常简单的问题要告诉你,这个问题困扰了我两个小时了。就是无法破解:D
我有一个ForEach遍历一个单元格区域,并将公式设置为COUNTIF公式...问题是,COUNTIF需要动态范围的标准(设置为rng2)以及位于M列的当前单元格行中的单元格。正在努力使公式工作......将发布一个我尝试过的想法(已经用同样的公式抖动了几个小时了)但是文字仍然保持红色哈哈

pr = WC.Cells(Rows.Count, "A").End(xlUp).Row
    Dim rng As Range
    Set rng = Application.Range("YMS!U2:U" & pr)
    
    For Each cel In rng.Cells
    cel.Formula = "CountIf(Range("rng2"), " & "M" & cel.Row)"
    Next cel
    On Error Resume Next

关于如何让我的COUNTIF公式适用于范围内的所有单元格,有什么想法吗?

kx7yvsdv

kx7yvsdv1#

VBA编写COUNTIF公式

Option Explicit

Sub WriteFormula()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim sws As Worksheet: Set sws = wb.Sheets("Unknown") ' adjust!
    Dim srg As Range: Set srg = sws.Range("Unknown") ' adjust!
    
    Dim dws As Worksheet: Set dws = wb.Sheets("YMS")
    Dim drg As Range
    Set drg = dws.Range("U2:U" & dws.Cells(dws.Rows.Count, "A").End(xlUp).Row)
    
    Dim dFormula As String: dFormula _
        = "=COUNTIF('" & sws.Name & "'!" & srg.Address & ",M" & drg.Row & ")"
    'Debug.Print dFormula
    
    drg.Formula = dFormula

End Sub
56lgkhnf

56lgkhnf2#

您缺少等号-此外,您必须将rng 2的地址放入公式中。

For Each cel In rng.Cells
       cel.Formula = "= CountIf(" & rng2.address & ", M" & cel.Row & ")"
    Next cel

相关问题