只对可见的那些求和

时间:2015-05-18 05:06:51

标签: excel-vba vba excel

最近,我一直试图编写一个VBA来帮助我总结一个列并按计数器除以得到平均值。但是,我有一个新的要求,即它只是总结那些可见的。我该怎么办?以下是我的代码,

Sub test3()
    Dim FinalRow As Long
    Dim Row As Long
    Dim counter As Integer
    Dim total As Double
    counter = 3
    total = 0
    Dim i As Double
    FinalRow = Range("C65536").End(xlUp).Row
        For Row = 3 To FinalRow
            If Not IsEmpty(ActiveSheet.Cells(counter, "C")) And Not IsEmpty(ActiveSheet.Cells(Row + 1, "C")) Then
                If ActiveSheet.Cells(counter, "B").Value = True Then
                    ActiveSheet.Cells(Row, "M").Value = 100
                    For i = counter To Row
                    If IsEmpty(ActiveSheet.Cells(i, "F")) Then
                            With ActiveSheet.Cells(i, "F")
                                .Value = Now
                                .NumberFormat = "dd/mm/yy"
                                If (.Value - .Offset(0, 2).Value) >= 0 Then
                            .Font.color = vbRed
                        Else
                            .Font.color = vbBlack
                        End If
                         End With
                         End If
                         Next i
                         End If
                    If (ActiveSheet.Cells(Row, "L").Value = 100) Then
                          For i = counter To Row
                    If IsEmpty(ActiveSheet.Cells(i, "F")) Then
                                With ActiveSheet.Cells(i, "F")
                                .Value = Now
                                .NumberFormat = "dd/mm/yy"
                                If (.Value - .Offset(0, 2).Value) >= 0 Then
                            .Font.color = vbRed
                        Else
                            .Font.color = vbBlack
                        End If
                         End With
                    End If
                    Next i
                End If
                If Not (ActiveSheet.Cells(counter, "B").Value) = True Then
                    ActiveSheet.Cells(counter, "M").Value = (Application.Sum(Range(ActiveSheet.Cells(counter, "L"), ActiveSheet.Cells(Row, "L")))) / (Row + 1 - counter)

                End If
                counter = Row + 1
             End If
        Next
End Sub

1 个答案:

答案 0 :(得分:1)

此测试代码适用于我,只需根据需要进行更改:

Sub TestSumme()
Dim Summe As Long
Summe = Application.WorksheetFunction.Sum(ThisWorkbook.Sheets(1).Range("A1:A6").SpecialCells(xlCellTypeVisible))
MsgBox (Summe)
End Sub