Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88029

VBA code to count distinct values based on multiple criteria

$
0
0

I have a data sheet "orders" which has data in the format

order no    Customer    Sales Executive Order Status    Order Date
211 nokia   john    cancelled   23-May-13
643 panasonic   andrew  fulfilled   23-May-13
209 samsung john    fulfilled   4-Apr-14
453 philips andrew  fulfilled   4-Apr-14
311 dell    mary    fulfilled   16-Apr-14
865 panasonic   andrew  fulfilled   16-Apr-14
201 apple   john    fulfilled   3-May-14
453 hp  mary    cancelled   3-May-14
205 nokia   john    fulfilled   4-May-14
643 philips andrew  fulfilled   4-May-14
312 lenovo  mary    fulfilled   22-May-14
204 apple   john    fulfilled   7-Jun-14
432 hp  mary    fulfilled   7-Jun-14
214 nokia   john    pending 25-Jun-14
754 panasonic   andrew  fulfilled   25-Jun-14

Above are the columns that matter out of the many columns in the order sheet.

I have another worksheet where I have the "Sales Executives" listed and want to know how many unique customers they had orders fulfilled by month

Sales Executive Apr-14  May-14  Jun-14
john    <value> <value> <value>
mary    <value> <value> <value>
andrew  <value> <value> <value>

I want to code to read the sales executive name in the row and month in the column and then give the answer like below

Sales Executive Apr-14  May-14  Jun-14
john    1   2   1
andrew  2   2   1
mary    1   1   1

I am looking for a vba code that can run this on a monthly basis. The above example is a sample set of the actual data.

I am relatively new to VBA and need help with the code.

It would be helpful if I get an explanation on the code as how it work as I need similar code to find how many products and total revenue generated by each sales executive for the months.

Thanks for your help in advance

EDIT (code from OP comment below):

Sub UniqueReport() 
Dim dict As Object 
Set dict = CreateObject("scripting.dictionary") 
Dim varray As Variant, element As Variant 
Dim lastrow As Long 

lastrow = Sheets("Orders").Range("N"& Rows.Count).End(xlUp).Row varray = Sheets("Orders").Range("N2:N"& lastrow).Value 

For Each element In varray 
    If dict.exists(element) Then 
        dict.Item(element) = dict.Item(element) + 1 
    Else 
        dict.Add element, 1 
    End If 
Next 

ActiveSheet.Range("P2").Value = dict.Count 
End Sub 

Viewing all articles
Browse latest Browse all 88029

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>