-1

谁能帮我用这个宏在一列中创建多个小计?任何帮助都会很棒。我在 Y 列中有一组数字。从第 16 行开始。数据每三行列出一次,直到该部分结束,然后有大约 30 行的间隙,然后又出现了。我想创建一个宏来计算每个部分中有多少个 >45 的数字。将总共​​ 2 行放在每个部分的最后一个数据点下方。在同一行的 X 列中,编号>45

Sub Sample()
    Dim result As Long, firstrow As Long, lastrow As Long
    Dim ws As Worksheet
    Dim rng As Range


    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Find Lastrow in Col Y
        lastrow = .Range("Y" & .Rows.Count).End(xlUp).Row
        '~~> Set First row
        firstrow = 16

        '~~> Set your range
        Set rng = .Range("Y" & firstrow & ":Y" & lastrow)

        '~~> Put relevant values
        .Range("x" & lastrow + 2).Value = "Total>45"
        .Range("y" & lastrow + 2).Value = _
        Application.WorksheetFunction.CountIf(rng, ">45")

    End With
End Sub
4

1 回答 1

0

尝试以下程序

并像这样向后循环到 ROW=1:

Sub setTotals()
Dim iRow As Integer
Dim iLastRow As Integer
Dim sFormulaTargetAddress As String

iLastRow = ActiveSheet.Range("Y" & ActiveSheet.Rows.Count).End(xlUp).Row


iRow = iLastRow
Do Until iRow = 1
If Range("Y" & iRow).Value <> "" Then
    '
    ' define the section
    sFormulaTargetAddress = "Y" & Range("Y" & iRow).End(xlUp).Row & ":Y" & iRow & ""
    '
    ' Put in the COUNTIF > 45 of the current section...
    '
    Range("Y" & iRow + 2).Formula = "=COUNTIF(" & sFormulaTargetAddress & ","">45"")"
    '        '
    'Range("X" & iRow + 2).Formula = "=COUNTIF(" & sFormulaTargetAddress & ","">45"")"
    Range("X" & iRow + 2).value="Numbers > 45"
    '
    ' find the next section
    iRow = Range("Y" & iRow).End(xlUp).Row
    If Range("Y" & iRow) <> "" Then iRow = Range("Y" & iRow).End(xlUp).Row
Else
    iRow = Range("Y" & iRow).End(xlUp).Row
End If
Loop


End Sub

截图

高温高压

菲利普

于 2013-04-11T08:25:47.720 回答