macros来计数并给出结果

谁能帮我。 我想统计有多less个数大于45,并把结果放在最后一个数据单元下面的3行。 让我们给它一个名字 – 称之为结果。 然后在结果的左边,我想把单词数字> 45.数据行的数量将会改变,所以当我在D列上运行macros时,它会find最后一个数据点并进行计算。 一些行将是空的。 谢谢您的帮助

它应该是这样的

50 20 100 120 45 30 30 

返回> 45 = 4

 Sub enter() Dim result As Integer Dim firstrow As Integer Dim lastwow As Integer Firstrow = d2 Result = ' Value of count Worksheets("sheet1").Range("c?").Value = "Total>45" Range("d100000").End(xlUp).Select End Sub 

尝试这个

 Sub Sample() Dim result As Long, firstrow As Long, lastrow As Long Dim ws As Worksheet Dim rng As Range '~~> Set this to the relevant worksheet Set ws = ThisWorkbook.Sheets("Sheet1") With ws '~~> Find Lastrow in Col D lastrow = .Range("D" & .Rows.Count).End(xlUp).Row '~~> Set First row firstrow = 1 '~~> Set your range Set rng = .Range("D" & firstrow & ":D" & lastrow) '~~> Put relevant values .Range("C" & lastrow + 3).Value = "Total>45" .Range("D" & lastrow + 3).Value = _ Application.WorksheetFunction.CountIf(rng, ">45") End With End Sub 

截图

在这里输入图像说明

这是一个让你通过任何数字,不只是45

 Sub MakeCount(lGreaterThan As Long) Dim lLast As Long With Sheet1 lLast = .Cells(.Rows.Count, 4).End(xlUp).Row .Cells(lLast + 3, 4).FormulaR1C1 = "=COUNTIF(R[-" & lLast + 1 & "]C:R[-3]C,"">""&RC[-1])" .Cells(lLast + 3, 3).Value = lGreaterThan .Cells(lLast + 3, 3).NumberFormat = """Number>""#" End With End Sub 

你不能使用工作表公式

=COUNTIF(A2:A7,">45")

或者像Siddharth Rout先生在他的回答中提到的那样

需要vba吗?

如果没有,函数=COUNTIF(C:C,">45")会给你你想要的答案。