Pages

Thứ Tư, 6 tháng 2, 2013

Hàm Sum nhiều tính năng

Mình gửi đến các bạn vài hàm Sum tự tạo nhằm đa dạng hóa tính năng cộng của hàm Sum

Đầu tiên là hàm SumType :
Chức năng :
- Cộng các ô có chứa hay không chứa công thức.
- Cộng các ô có hay không có màu nền
- Cộng các ô có hay không có màu Font chữ
- Cộng các ô có hay không có Font chữ in đậm
- Bỏ qua các ô bị lỗi khi cộng
Link hàm: Sum.xls
Code:
Public Function SumType(TypeFunc As ByteOpt As BooleanRng1 As Range_
      Optional Rng2 
As RangeOptional Rng3 As RangeOptional Rng4 As Range_
      Optional Rng5 
As Range) As Double
Dim Cell 
As RangeSumT As DoubleFor Each Cell In Rng1If Not IsError(CellThen
    
If IsNumeric(CellThen
    Select 
Case TypeFunc
    
Case 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co hay khong co font in dam.'
        
If Cell.Font.Bold Opt Then SumT SumT Cell
    End Select
    End 
IfEnd IfNext'---------------------------------------------------------------------'If Not Rng2 Is Nothing ThenFor Each Cell In Rng2If Not IsError(CellThen
    
If IsNumeric(CellThen
    Select 
Case TypeFunc
    
Case 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co hay khong co font in dam.'
        
If Cell.Font.Bold Opt Then SumT SumT Cell
    End Select
    End 
IfEnd IfNext
End 
If'---------------------------------------------------------------------'If Not Rng3 Is Nothing ThenFor Each Cell In Rng3If Not IsError(CellThen
    
If IsNumeric(CellThen
    Select 
Case TypeFunc
    
Case 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co hay khong co font in dam.'
        
If Cell.Font.Bold Opt Then SumT SumT Cell
    End Select
    End 
IfEnd IfNext
End 
If'---------------------------------------------------------------------.'If Not Rng4 Is Nothing ThenFor Each Cell In Rng4If Not IsError(CellThen
    
If IsNumeric(CellThen
    Select 
Case TypeFunc
    
Case 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co hay khong co font in dam.'
        
If Cell.Font.Bold Opt Then SumT SumT Cell
    End Select
    End 
IfEnd IfNext
End 
If'---------------------------------------------------------------------.'If Not Rng5 Is Nothing ThenFor Each Cell In Rng5If Not IsError(CellThen
    
If IsNumeric(CellThen
    Select 
Case TypeFunc
    
Case 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex Opt Then SumT SumT Cell
    
Case 'cong cac cell co hay khong co font in dam.'
        
If Cell.Font.Bold Opt Then SumT SumT Cell
    End Select
    End 
IfEnd IfNext
End 
IfSumType SumT
End 
Function