我需要一个函数来计算J列中每个项目的数量,并在K列中显示结果。但是我在下面显示的代码一直在说,标准部分RC-2是错误的。在countif函数之后,我需要它能够自动填充给定的任何行,这样我就可以将此代码应用于其他文件。
我使用Macro生成了一些启动代码。也可以在前面尝试一下: paste_countPTD = Worksheetfunction.CountIf(paste_conPTD,RC-2)。
标准部分似乎是错误的。
Dim paste_conPTD As Range
Set paste_conYTD = Range("J2:J" & Range("A" & Rows.Count).End(xlUp).Row)
Range("K1").Select
ActiveCell.FormulaR1C1 = "Count_PTD"
Range("K2").Worksheetfunction.countif(paste_conPTD,RC[-2])我很感谢任何让这段代码工作的建议。对列执行countif并自动填充公式。
发布于 2019-08-29 12:56:47
你可以试试这段代码
Dim paste_conPTD As Range
Set paste_conYTD = Range("J2:J" & Range("A" & Rows.Count).End(xlUp).Row)
Range("K1") = "Count_PTD"
Dim iRng as Range
For each iRng in paste_conPTD
iRng.Offset(0,1) = Worksheetfunction.Countif(paste_conPTD, iRng)
Next iRng为了给你一些提示,我们需要遍历paste_conYTD区域中的每个单元格,这就是iRng的用武之地。我们不能像paste_conYTD = <some formula>一样告诉Excel,并假设Excel知道我们希望使用公式为每个单元格计算它。Excel迭代有几种方式,我们可以根据场景选择最容易应用的方式。
For each ... in ... NextFor ... Nexthttps://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/fornext-statement
Do... Loophttps://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/doloop-statement
While... Wend发布于 2019-08-29 17:09:01
如果你想要单元格中的实际公式,试试这个。Dim paste_conPTD As Range
Set paste_conYTD = Range("J2:J" & Range("A" & Rows.Count).End(xlUp).Row)
Range("K1").Value = "Count_PTD"
paste_conYTD.Offset(, 1).FormulaR1C1 = "=COUNTIF(" & paste_conYTD.Address(ReferenceStyle:=xlR1C1) & ",RC[-2])"https://stackoverflow.com/questions/57700666
复制相似问题