Look for name in row then count categories below it and add them up

Nick70

Active Member
Joined
Aug 20, 2013
Messages
299
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a set of data under different analyst names in Sheet2 and Sheet3.

Something like below:

John Lucy

pass fail
fail pass
pass error
error error
pass pass
fail pass

I would like to have a macro that summarizes data under John and Lucy (found in Sheets 2 and 3) and gives the totals in Sheet1.

So for example in Sheet1 we will have totals:
John
pass 3
fail 2
error 1

Lucy
pass 3
fail 1
error 2

The analyst names are found in Row 9 of Sheets 2 and 3 (let's say a range K9 to O9)
The data to be summarized will be under those names (let's say in range K29:O50)

How can I summarize in Sheet1 how many pass, fails, errors are under John and how many under Lucy if the analyst names are found in range K9 to O9 of Sheet2 and Sheet3 and the data to add up is found under the analysts names in range K29:O50 of Sheet2 and Sheet3?

I would like to have macro and not use a Pivot.

Thanks,
N.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi Nick,

does it have to be a VBA macro or would you be interested in a solution with a formula?

Regards,
Elaszat
 
Upvote 0
If your data is like this on sheets 2 and 3

Book1
AKLMNO
1
8
9John1John2John3John4John5
10passfailpasserrorpass
11failpasserrorpassfail
12passerrorpassfailpass
13errorpassfailpassfail
14passfailpassfailpass
15failpassfailpassfail
Sheet2


Book1
AKLMNO
1
8
9Lucy1Lucy2Lucy3Lucy4Lucy5
10passfailpasserrorpass
11failpasserrorpassfail
12passerrorpassfailpass
13errorpassfailpassfail
14passfailpassfailpass
15failpassfailpassfail
Sheet3


Try this macro, the results will be on sheet1 in columns A and B

Book1
AB
1
2John1
3pass3
4fail2
5error1
6
7John2
8fail2
9pass3
10error1
11
12John3
13pass3
14error1
15fail2
16
17John4
18error1
19pass3
20fail2
21
22John5
23pass3
24fail3
25
26Lucy1
27pass3
28fail2
29error1
30
31Lucy2
32fail2
33pass3
34error1
35
36Lucy3
37pass3
38error1
39fail2
40
41Lucy4
42error1
43pass3
44fail2
45
46Lucy5
47pass3
48fail3
Sheet1


VBA Code:
Sub count_categories()
  Dim sh1 As Worksheet, sh2 As Worksheet, dic As Object
  Dim a() As Variant, b() As Variant, ky As Variant, arr As Variant, sh As Variant
  Dim n As Long, i As Long, j As Long
 
  Set sh1 = Sheets("Sheet1")
  Set dic = CreateObject("Scripting.Dictionary")
 
  sh1.Range("A2:B" & Rows.Count).ClearContents
  arr = Array("Sheet2", "Sheet3")
  n = 1
 
  For Each sh In arr
    
    Set sh2 = Sheets(sh)
    b = sh2.Range("K9", sh2.Range("O" & sh2.Range("O" & Rows.Count).End(xlUp).Row)).Value
    If n = 1 Then ReDim a(1 To (UBound(b, 1) * UBound(b, 2)) * (UBound(arr) + 10), 1 To 2)
    
    For j = 1 To UBound(b, 2)
      dic.RemoveAll
      For i = 2 To UBound(b, 1)
        dic(b(i, j)) = dic(b(i, j)) + 1
      Next i
      a(n, 1) = b(1, j)
      n = n + 1
      For Each ky In dic.keys
        a(n, 1) = ky
        a(n, 2) = dic(ky)
        n = n + 1
      Next ky
      n = n + 1
    Next j
    
  Next sh
 
  sh1.Range("A2").Resize(n, 2).Value = a()
End Sub
 
Upvote 0
Hi,

after seeing the code by @DanteAmor I'm not sure I understood your request porperly. But here is what I have come up with:
Cell Formulas
RangeFormula
A2:E4A2=IF(A$1=HLOOKUP(A$1,Sheet2!$K$9:$O$9,1,FALSE),COUNTIF(OFFSET(Sheet2!$K$29:$O$50,0,COLUMN(A$1)-1,,1),$F1))


The data so far is in Sheet 2, like this:
Book1
KLMNO
9JohnLucyAdamSteveAubrey
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29errorpassfailfailfail
30failfailpasserrorpass
31failfailerrorerrorerror
32passpasspasserrorpass
33passerrorfailpasserror
34errorerrorfailerrorerror
35errorerrorfailerrorpass
36failpasspasspasserror
37passerrorfailpasspass
38errorpasspasspasserror
39failerrorerrorfailerror
40errorerrorerrorpasspass
41passpasspasspasspass
42passfailerrorfailpass
43passpassfailpassfail
44failfailfailerrorfail
45failerrorfailerrorfail
46passerrorerrorfailfail
47failpasserrorfailerror
48failpasserrorfailerror
49failpasserrorerrorfail
50passerrorpassfailerror
Sheet2


Regards,
Elaszat
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
You're welcome and yeah, thanks for the feedback. :)
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,676
Members
448,977
Latest member
moonlight6

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