For i = LBound(brandArray) To UBound(brandArray)
With Worksheets(FirstSheet)
'looks up the gender
female = WorksheetFunction.IfError(WorksheetFunction.index(.Range(.Cells(2, Female_Column), .Cells(LastRow, Female_Column)), _
WorksheetFunction.Match(brandArray(i), .Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), 0)), "")
male = WorksheetFunction.IfError(WorksheetFunction.index(.Range(.Cells(2, Male_Column), .Cells(LastRow, Male_Column)), _
WorksheetFunction.Match(brandArray(i), .Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), 0)), "")
If female = "Y" Then
'numerator1 looks at i in brandArray, with sizes 2,3, and 4, with purpose code 1, for both red and blue.
numerator1 = WorksheetFunction.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), red, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4), _
.Range(.Cells(2, purposeColumn), .Cells(LastRow, purposeColumn)), 1)) + _
WorksheetFunction.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), blue, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4), _
.Range(.Cells(2, purposeColumn), .Cells(LastRow, purposeColumn)), 1))
'numerator2 at i in brandArray, with sizes 2, 3, and 4, with material codes 1, 2, 3, 7, 8, 9, 10, for both red and blue
numerator2 = Application.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), red, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4), _
.Range(.Cells(2, materialColumn), .Cells(LastRow, materialColumn)), Array(1, 2, 3, 7, 8, 9, 10))) + _
Application.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), blue, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4), _
.Range(.Cells(2, materialColumn), .Cells(LastRow, materialColumn)), Array(1, 2, 3, 7, 8, 9, 10)))
'numerator_intersection is the intersection of the two sets (purpose code and material code)
numerator_intersection = Application.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), red, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4), _
.Range(.Cells(2, purposeColumn), .Cells(LastRow, purposeColumn)), 1, _
.Range(.Cells(2, materialColumn), .Cells(LastRow, materialColumn)), Array(1, 2, 3, 7, 8, 9, 10))) + _
Application.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), blue, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4), _
.Range(.Cells(2, purposeColumn), .Cells(LastRow, purposeColumn)), 1, _
.Range(.Cells(2, materialColumn), .Cells(LastRow, materialColumn)), Array(1, 2, 3, 7, 8, 9, 10)))
'combined both numerators as OR condition for union of sets (to avoid overcounting)
numerator = numerator1 + numerator2 - numerator_intersection
'denominator consists of ALL brand i with sizes 2, 3, and 4, in either red or blue
denominator = WorksheetFunction.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), red, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4))) + _
WorksheetFunction.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), blue, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4)))
'otherwise there is no gender
Else: denominator = -1
answer = numerator / denominator
End If
End With
So in numerator2 should we have:
numerator2 = Application.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), red, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4), _
.Range(.Cells(2, materialColumn), .Cells(LastRow, materialColumn)), Application.Transpose(Array(1, 2, 3, 7, 8, 9, 10)))) + _
Application.Sum(Application.CountIfs(.Range(.Cells(2, colorColumn), .Cells(LastRow, colorColumn)), blue, _
.Range(.Cells(2, brandColumn), .Cells(LastRow, brandColumn)), brandArray(i), _
.Range(.Cells(2, sizeColumn), .Cells(LastRow, sizeColumn)), Array(2, 3, 4), _
.Range(.Cells(2, materialColumn), .Cells(LastRow, materialColumn)), Application.Transpose(Array(1, 2, 3, 7, 8, 9, 10))))
Bookmarks