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 Byte, Opt As Boolean, Rng1 As Range, _
      Optional Rng2 
As Range, Optional Rng3 As Range, Optional Rng4 As Range, _
      Optional Rng5 
As Range) As Double
Dim Cell 
As Range, SumT As DoubleFor Each Cell In Rng1If Not IsError(Cell) Then
    
If IsNumeric(Cell) Then
    Select 
Case TypeFunc
    
Case 1 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula = Opt Then SumT = SumT + Cell
    
Case 2 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 3 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 4 '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(Cell) Then
    
If IsNumeric(Cell) Then
    Select 
Case TypeFunc
    
Case 1 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula = Opt Then SumT = SumT + Cell
    
Case 2 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 3 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 4 '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(Cell) Then
    
If IsNumeric(Cell) Then
    Select 
Case TypeFunc
    
Case 1 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula = Opt Then SumT = SumT + Cell
    
Case 2 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 3 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 4 '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(Cell) Then
    
If IsNumeric(Cell) Then
    Select 
Case TypeFunc
    
Case 1 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula = Opt Then SumT = SumT + Cell
    
Case 2 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 3 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 4 '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(Cell) Then
    
If IsNumeric(Cell) Then
    Select 
Case TypeFunc
    
Case 1 'cong cac cell dang value hay formula.'
        
If Cell.HasFormula = Opt Then SumT = SumT + Cell
    
Case 2 'cong cac cell co mau hay khong co mau nen.'
        
If Cell.Interior.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 3 'cong cac cell co mau hay khong co mau font chu.'
        
If Cell.Font.ColorIndex > 0 = Opt Then SumT = SumT + Cell
    
Case 4 '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