maabadi
Well-known Member
- Joined
- Oct 22, 2012
- Messages
- 2,681
- Office Version
- 2019
- 2016
- Platform
- Windows
Hi Guys.
I want to write UDF or macro to count sumif result if more than one criteria.
I write both But have Problems.
1. User Defined Function:
I Know have problems. but I don't see it when write at Insert function.
2. Macro Shows missmatch for Sumresult
Please help
I want to write UDF or macro to count sumif result if more than one criteria.
I write both But have Problems.
1. User Defined Function:
VBA Code:
Function CountSumResult(InputRange As Range, Criteria As Variant, SumRange As Range, SumCriteria As Long) As Long
Dim Cell As Range
Dim SumResult As Long
Set CountSumResult = 0
For Each Cell In InputRange
If WorksheetFunction.CountIf(Range(Cells(InputRange.Row, Cell.Column).Address, Cells(Cell.Row, Cell.Column).Address), Cell.Value) = 1 Then
WorksheetFunction.SumIf(SumRange, Criteria) = SumResult
WorksheetFunction.CountIf(SumResult, SumCriteria) = CountSum
CountSumResult = CountSum + CountSumResult
Else
CountSumResult = CountSumResult
End If
Next Cell
End Function
2. Macro Shows missmatch for Sumresult
VBA Code:
Sub Countsssss()
Dim InputRange As Range
Dim Criteria As Variant
Dim SumRange As Range
Dim CountSumResult As Long
Dim SumCriteria As Variant
Dim Cell As Range
Dim SumResult As Long
Dim CountSum As Long
CountSumResult = 0
Set InputRange = Range("A2:A22")
Set SumRange = Range("C2:C22")
SumCriteria = " >= " & 170
For Each Cell In InputRange
If WorksheetFunction.CountIf(Range(Cells(InputRange.Row, Cell.Column).Address, Cells(Cell.Row, Cell.Column).Address), Cell.Value) = 1 Then
SumResult = WorksheetFunction.SumIf(SumRange, Range("$B$2")) + WorksheetFunction.SumIf(SumRange, Range("$B$3"))
Debug.Print SumResult
CountSum = WorksheetFunction.CountIf(SumResult, SumCriteria)
Debug.Print CountSum
CountSumResult = CountSum + CountSumResult
Debug.Print CountSumResult
Else
CountSumResult = CountSumResult
End If
Next Cell
End Sub
Please help