0

谁能帮我。我想计算有多少数字 > 45,并将结果放在最后一个数据单元格下方 3 行。让我们给它一个名字——称之为结果。然后在结果的左侧,我想输入数字 > 45。数据行的数量会改变,所以当我在 D 列上运行宏时,它会找到最后一个数据点并进行计算。一些行将是空的。谢谢您的帮助

它应该是这样的

     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
4

4 回答 4

2

尝试这个

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

截屏

在此处输入图像描述

于 2013-04-10T16:08:27.217 回答
1

这是一个可以让你通过任何数字,而不仅仅是 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
于 2013-04-10T18:52:08.817 回答
0

你不能使用像这样的工作表公式吗

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

或者,正如Siddharth Rout先生在他的回答中所建议的那样,在 VBA 中

于 2013-04-10T16:10:11.000 回答
0

需要vba吗?

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

于 2013-04-10T16:10:17.220 回答