宏来计算和给出结果

时间:2013-04-10 15:56:58

标签: excel excel-vba excel-formula vba

任何人都可以帮助我。我想算一下有多少数字> 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 个答案:

答案 0 :(得分: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

<强>截图

enter image description here

答案 1 :(得分: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

答案 2 :(得分:0)

你不能使用像

这样的工作表公式

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

另外,在VBA中,先生 Siddharth Rout 在他的回答中建议

答案 3 :(得分:0)

需要vba吗?

如果没有,函数=COUNTIF(C:C,">45")将为您提供所需的答案。