Countifs with dynamically altering criteria

Allan91

New Member
Joined
Dec 17, 2020
Messages
33
Office Version
  1. 2019
Platform
  1. Windows
Hi Guys,

I am trying to create a formula where I can draw data from a dataset which has 3 fixed ranges, 2 fixed criterion and 1 dynamic criteria.

As I have shown in the uploaded images, there are the months, types of items and customers. All this data is drawn by a VBA code to the raw data dynamically which I use to transfer to the clean data. Almost everything I need does work with sumifs but for the number of customers part I need to use a count function to draw data. For example I want to draw the number of customers who bought MUGS in JANUARY. The problem is that each month's customers are bound to be different in every insantce and thus I cannot specify a customer name to look for and count. If countifs worked like sumifs it would be easy but with a count function I'm stuck.

Please note that a single customer can buy the same item within the same month. So accounting only for the item and the month for the countifs function will not suffice.

Looking forward to your kind help!
 

Attachments

  • Clean Data_1.PNG
    Clean Data_1.PNG
    30.1 KB · Views: 61
  • Raw Data_1.PNG
    Raw Data_1.PNG
    103.2 KB · Views: 62

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Please provide all relevant links

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
I used @MickG Solution at this Post and modify it.
VBA to find unique from multiple columns and count on multiple criteria. Anybody please

this is code:
VBA Code:
Sub MG28Dec42()
Dim Ray As Variant, Rw As Long, Ac As Long, n As Long, Dic As Object, Q As Variant
Dim Cell As Range
Ray = Range("b1").CurrentRegion.Resize(, 6)
ReDim nray(1 To UBound(Ray, 1) * UBound(Ray, 2), 1 To 3)
'nray(1, 1) = "Month": nray(1, 2) = "Item": nray(1, 3) = "NoOfCustomers"
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
n = 1
'If you familiar with "Scripting Dictionary" you will know it has
'"Keys" and "Items" relating to those Keys:=
For Rw = 2 To UBound(Ray, 1)
For Ac = 2 To UBound(Ray, 2)
If Not Dic.Exists(Ray(Rw, 1) & Ray(Rw, 3)) Then

'"n" represents the Index of every new Unique value in dictionary "Keys"
'This "n" value is used to define each new row in the results Array "nRay"
n = n + 1
'Fill first Row of new Unique Results
nray(n, 1) = Ray(Rw, 1): nray(n, 2) = Ray(Rw, 3): nray(n, 3) = 1

'Array(n,1) is the "ITEM" of each unique value (KEY) found indata and
'represents the value "n" giving the row location of each new value placed in array "nRay" and
'"1" which is the first count of each unique value found
' This count is increases in the Else statement, below when another count of the
'same Unique value is found !!
Dic.Add Ray(Rw, 1) & Ray(Rw, 3), Array(n, 1)

Else
' The variable "Q" is used to represent the "ITEM" of each Unique (KEY)
' so "Q" is the "ITEM" of the "KEY" :- Ray(Rw, 1) & Ray(Rw, Ac)
Q = Dic(Ray(Rw, 1) & Ray(Rw, 3))

'There are 2 values in "Q", "Q0" the first value "n" and "Q1" the count of each subsequent
'Unique value found
'That count is increased by 1 each time another value, for that unique "KEY" is found
Q(1) = Q(1) + 1

'Column 3 of the Results array nRay is increased by 1
nray(Q(0), 3) = Q(1)

'The item resulting to "Q" is updated.
Dic(Ray(Rw, 1) & Ray(Rw, 3)) = Q

End If
Next Ac
Next Rw
Range("U1").Resize(n, 2) = nray

For Each Cell In Range("Y3:Y" & Cells(Rows.Count, 22).End(xlUp).Row)
Cell.Value = Application.WorksheetFunction.CountIfs(Range("B3:B" & Cells(Rows.Count, 2).End(xlUp).Row), _
Range("U" & Cell.Row), Range("D3:D" & Cells(Rows.Count, 4).End(xlUp).Row), Range("V" & Cell.Row))
Cell.Offset(0, 1).Value = Application.WorksheetFunction.SumIfs(Range("F3:F" & Cells(Rows.Count, 6).End(xlUp).Row), Range("B3:B" & _
Cells(Rows.Count, 2).End(xlUp).Row), Range("U" & Cell.Row), Range("D3:D" & Cells(Rows.Count, 4).End(xlUp).Row), Range("V" & Cell.Row))
Next Cell

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,723
Messages
6,174,123
Members
452,546
Latest member
Rafafa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top