2015-05-18 26 views
0

最近,我一直在嘗試編寫一個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 
+0

這可能會給你一個想法[如何使用VBA宏來概括唯一可見的細胞](https://support.microsoft.com/en-us/kb/150363) – Peter

+0

哈哈....感謝彼得。但現在我完全失去了如何申請我的情況。有點混亂。希望有人能給我兩個提示。 – kai

+0

你想'小計'而不是'總和' – Rory

回答

1

這testcode工作對我來說,只是改變它,你需要它:

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