Count Distinct Values for 2 criteria in Excel using VBA

jpb7887

New Member
Joined
May 31, 2018
Messages
2
Hello

I am new in VBA and I am looking to build a macro to count the number of stores a responsible (let's say K) sells to, for each brand.
In this example, K sells to 1 store the brand 1 and 1 store the brand 2. (so a total of 3 sales but to 2 stores only).
Any help would be really appreciated
Thanks

[TABLE="width: 500"]
<tbody>[TR]
[TD]
[TABLE="width: 147"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Responsible[/TD]
[/TR]
[TR]
[TD]K[/TD]
[/TR]
[TR]
[TD]K[/TD]
[/TR]
[TR]
[TD]K[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]S[/TD]
[/TR]
[TR]
[TD]S[/TD]
[/TR]
[TR]
[TD]S[/TD]
[/TR]
[TR]
[TD]S[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 306"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Store Name[/TD]
[/TR]
[TR]
[TD] TWCM Unley[/TD]
[/TR]
[TR]
[TD] HealthSave Stirling[/TD]
[/TR]
[TR]
[TD] HealthSave Stirling[/TD]
[/TR]
[TR]
[TD]Pharmacy 360 Black Rock[/TD]
[/TR]
[TR]
[TD]Mulqueeny Midnight Pharmacy[/TD]
[/TR]
[TR]
[TD]Alfred Healthsmart [/TD]
[/TR]
[TR]
[TD]Ramsay CBD [/TD]
[/TR]
[TR]
[TD]Scott Dibben[/TD]
[/TR]
[TR]
[TD]SP Pitt St[/TD]
[/TR]
[TR]
[TD]PL George St[/TD]
[/TR]
[TR]
[TD]PL George St[/TD]
[/TR]
[TR]
[TD]TW Chatswood Chase[/TD]
[/TR]
[TR]
[TD]CWH Chatswood[/TD]
[/TR]
[TR]
[TD]PL Wynard[/TD]
[/TR]
[TR]
[TD]PL World Square[/TD]
[/TR]
[TR]
[TD]PL World Square[/TD]
[/TR]
[TR]
[TD]SP Pitt Street[/TD]
[/TR]
[TR]
[TD]SP Pitt Street[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 56"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Brand[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Perhaps a Pivot Table as shown below.

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:rgb(136, 136, 136)][th] [/th][th]
H
[/th][th]
I
[/th][th]
J
[/th][/tr]
[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
1
[/td][td=bgcolor:rgb(217, 225, 242)]Sum of Brand[/td][td=bgcolor:rgb(217, 225, 242)]Column Labels[/td][td=bgcolor:rgb(217, 225, 242)][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
2
[/td][td=bgcolor:rgb(217, 225, 242)]Row Labels[/td][td=bgcolor:rgb(217, 225, 242)]
1
[/td][td=bgcolor:rgb(217, 225, 242)]
2
[/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
3
[/td][td]B[/td][td]
10
[/td][td]
2
[/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
4
[/td][td] Alfred Healthsmart[/td][td]
1​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
5
[/td][td] CWH Chatswood[/td][td]
1​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
6
[/td][td] Mulqueeny Midnight Pharmacy[/td][td]
1​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
7
[/td][td] Pharmacy 360 Black Rock[/td][td]
1​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
8
[/td][td] PL World Square[/td][td]
2​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
9
[/td][td] PL Wynard[/td][td]
1​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
10
[/td][td] Ramsay CBD[/td][td]
1​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
11
[/td][td] Scott Dibben[/td][td]
1​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
12
[/td][td] SP Pitt Street[/td][td]
1​
[/td][td]
2​
[/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
13
[/td][td]K[/td][td]
1
[/td][td]
4
[/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
14
[/td][td] HealthSave Stirling[/td][td][/td][td]
4​
[/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
15
[/td][td] TWCM Unley[/td][td]
1​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
16
[/td][td]S[/td][td]
1
[/td][td]
6
[/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
17
[/td][td] PL George St[/td][td][/td][td]
4​
[/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
18
[/td][td] SP Pitt St[/td][td]
1​
[/td][td][/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
19
[/td][td] TW Chatswood Chase[/td][td][/td][td]
2​
[/td][/tr]


[tr=bgcolor:rgb(255, 255, 255)][td=bgcolor:rgb(136, 136, 136)]
20
[/td][td=bgcolor:rgb(217, 225, 242)]Grand Total[/td][td=bgcolor:rgb(217, 225, 242)]
12
[/td][td=bgcolor:rgb(217, 225, 242)]
12
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet1[/td][/tr][/table]
 
Last edited:
Upvote 0
Thank you, I know it is doable in "simple excel"but it has to be done with VBA as it is for my manager who wants to "press the button" and refresh the data every month
 
Upvote 0
Use the Macro Recorder and then post your code and we will modify as necessary.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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