0

我在完成宏时遇到问题。它使用 COUNTIF 函数并根据来自 B2 的数据检查整个 A 列(A:A - 无排除项)。我想在 C 列中进行自动填充,直到 B 列中的最后一个值 - 就像下图所示:

在此处输入图像描述

谁能帮我我应该在我的代码中添加什么来使这个自动填充成为可能?

Sub Countif()

Range("C2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-2],RC[-1])"
Selection.AutoFill Destination:=Range("C2:C10"), Type:=xlFillDefault
Range("C2:C10").Select

End Sub
4

2 回答 2

1

如果您想使用 VBA 实现,请尝试以下代码。只需运行宏,它就会填满 B 列中的 C 列 uptil 值。

Sub sample()

Dim LastRowColumnB As Long
LastRowColumnB = Range("B65000").End(xlUp).Row

For i = 2 To LastRowColumnB
    Cells(i, 3) = Application.CountIf(Range("A:A"), Cells(i, 2))
Next
End Sub
于 2013-03-15T17:30:09.960 回答
0

您可以像这样获取列的最后一行:

Dim LastRowColumnB as Long
LastRowColumnB = Activesheet.Cells(Activesheet.Rows.Count, "B").End(xlUp).Row

然后修改您的自动填充以使用该值。

Selection.AutoFill Destination:=Range("C2:C" & LastRowColumnB), Type:=xlFillDefault
于 2013-03-15T17:19:28.567 回答