URGENT: Help with counting how often a value occurs

dmshott

New Member
Joined
Aug 28, 2015
Messages
4
Please help! How can I get a total number of each occurrence by state if my data currently looks like this?

ACO Service Area
AZ
AZ
AZ
AR
AR
AZ, NM
AZ
AZ
AZ
AZ
AZ, TX
CA
CA
CA
CA
CA
CA
CA
CA, MO, TX
CA
CA
CA, MS, OH
CA
CA
CA
CA
CA, FL
CA
CA
CA
CA, DC, FL, GA, LA, NC, SC, TN, TX
CA
CA, PA, TN
CA, IN, MI
CA, IL, IN, IA
CA
OR, WA
CA
CA
IN, OH
IN
CA
CA
CO
CO
CO
CT, NY
CT
CT
CT, NY
CT
CT
CT, NY
DE, MD
NJ
GA, SC
AL, FL
FL
FL
FL, SC, GA
FL
FL
FL
LA
FL
FL
FL
FL
FL
FL
FL
FL
FL
AL, MS, FL
FL, MO, NC, RI
FL
FL
FL
FL
FL
FL
FL
FL
FL, NJ, PA
FL, TX
FL
FL
FL
FL
FL
MS
FL
FL
FL
FL
FL
FL
FL
GA
GA
GA
GA, SC
DC, MD
FL, GA
AL, GA
AL
GA, IN, OH
GA
FL, GA
GA
GA
GA
ID, OR
ID, OR
IL
IL
IL
IL, MA
IL
IL
FL
IL
IL
IL
IL, IN
IL
IL
IL, NC
IL
IL
OH
IN, MI
IL, IN, KY
IN
IL, IN
IN
IN, MI
IL, IN
IN
IN, MI
IN
IN, MI
IL, IA
IA
IA
IL, IA, WI
AR
AR
AR, OK
KS
AR, OK
KS, MO
KS
KS, MO
KS
KS
KY, LA, TN
IL, KY
KY
PA
IN, KY
KY
IN, KY
KY
IN, KY
TN
LA
LA, MS
LA, MS, TN
ME
ME
ME, NH
MD
MD
DE, MD, PA
AR, MA, NY
MD, PA
MD
KY, OH, NY, SC, VA
MD, PA
DC, MD
MD
DE, MD, VA
DE, MD
MD, WV, PA
DC, MD
DC, MD
MD
DC, MD
MD, PA, WV
VA
MD, PA, WV
CT, IL, IA, MA, NY, OH, PA
CT, MA
MA
MA
MA, NH
MA
MA
MA
MA, NH
MA, NH
CA, MA, PA, TX, UT, WV, CT, IA
CT, MA
MA, RI
MA
MA, NH
MI
MI
MI
MI
MI
MI
MI
MI
MI
MI
MI
MI
GA, MI
FL, MI, MO, OH, TX, VA, WI
MN
MN, ND, WI
MN, WI
MN
MS
MS
AL, MS
GA, KY, MS, NC, TN, VA
IL, MO
MO
AR, CO, KY, MO, TN
KS, MO
KS, MO
AR, MO
AR, MO, OK
IL, MO
IL, MO
MT, WY
MT, WY
IA, NE
IA, NE
NE
NV
AZ, CA, NV, MO
CA, NV
NV
ME, MA, NH
NH, VT
NJ
NJ, PA
NJ, PA
NJ
NJ
NJ, PA
NJ
NJ
NJ, NY
NJ
NJ
NJ
DE, NJ, PA
NJ
NJ
NJ
NJ
NJ
NJ
NM
NY
CT, NY
NY
NY
NY
NY
NY
NY
NY
NY
NJ, NY
NY, PA
NY, PA
CT, NY
NY
NY
NY
NY
NY
NY
NJ, NY
FL, NJ, NY
NY
NY
NY
NY
CT, NY
NC
NC
ND, SD
OH
OH
OH
OH
KY, OH
OH
OH
OH
MI, OH
MI, OH
OH
KS, OK
OK
OR
NJ, PA
NY, PA
PA
PA
PA
NJ, PA
PA
MD, PA
PR
PR
MA, RI
MA, RI
NC
SC
NC
NC, SC, VA
NC, SC
NC
NC
NC
NC
SC
NC
NC
NC
NC
TN, VA
TN
TN
GA, TN
AL
IN, KY
TN
IN
TN, VA
TN
TN
TX
TX
TX
TX
LA, TX, OK
TX
TX
TX
TX
TX
NM, TX
TX
TX
TX
TX
TX
TX
OK, TX
TX
TX
TX
TX
TX
TX
TX
TX
TX
TX
NV, UT
UT
VT
NH, VT
NH, VT
VA
NC
VA, WV
VA
VA
VA, WV
VA
VA
TX
DC, MD
VA, WV
VA
VA
WA
WA
AK, MT, WA
WI
WI
WI
IL, WI
WI
WI

<colgroup><col style="width: 100px"></colgroup><tbody>
</tbody>
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi,

Try the following formula in C1:

=COUNTIF(A:A,"*"&B1&"*")

This is considering the state names are in column A and the values to lookup is in column B (B1)
 
Upvote 0
There are no values to look up in B1. I am trying to count the occurrences of the state names, but certain cells have more than one state and are divided by commas.
 
Upvote 0
cbatrody's formula accounts for cells where you have more than 1 state name. B1 is the cell where you have the state name you want to count. SO instead of having:
=COUNTIF(A:A,"*AZ*") the formula is =COUNTIF(A:A,"*"&B1&"*"), where B1 has the state name in it. I am assuming you want to know how many times AZ, AR etc are present in column A and cbatrody's formula does exactly that, state by state. You only need to put the state name you want in B1 and that way you can easily change it.
 
Upvote 0
dmshott,

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?

Here is a macro solution for you to consider that uses two arrays in memory, and, the Scripting.Dictionary, and, will write the sorted results in columns C, and, D.

You can change the raw data worksheet name in the macro.

Sample raw data, and, results (not all rows are shown for brevity):


Excel 2007
ABCD
1ACO Service AreaService AreaCount
2AZAL6
3AZAR10
4AZAZ10
5ARCA32
6ARCO4
7AZ, NMCT14
8AZDC7
9AZDE5
10AZFL47
11AZGA18
12AZ, TXIA9
13CAID2
14CAIL27
15CAIN22
16CAKS9
17CAKY14
18CALA7
19CAMA20
20CA, MO, TXMD21
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub GetUniqueCount()
' hiker95, 08/28/2015, ME878969
Dim a As Variant, d As Object, s, o As Variant
Dim lr As Long, i As Long, j As Long
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Cells(.Rows.Count, 1).End(xlUp).Row
  a = .Range("A2:A" & lr)
  .Columns("C:D").ClearContents
  With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For i = LBound(a, 1) To UBound(a, 1)
      If InStr(a(i, 1), ", ") Then
        s = Split(a(i, 1), ", ")
        For j = LBound(s) To UBound(s)
          If .Exists(s(j)) Then
            .Item(s(j)) = .Item(s(j)) + 1
          Else
            .Add s(j), 1
          End If
        Next j
      Else
        If Not .Exists(a(i, 1)) Then
          .Add a(i, 1), 1
        Else
          .Item(a(i, 1)) = .Item(a(i, 1)) + 1
        End If
      End If
    Next i
    o = Application.Transpose(Array(.Keys, .Items))
  End With
  .Range("C1").Resize(, 2).Value = Array("Service Area", "Count")
  .Range("C2").Resize(UBound(o, 1), UBound(o, 2)) = o
  .Range("C2:D" & UBound(o, 1)).Sort key1:=.Range("C2"), order1:=1
  .Columns("C:D").AutoFit
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the GetUniqueCount macro.
 
Last edited:
Upvote 0
dmshott,

Here is an updated macro solution for you to consider that uses two arrays in memory, and, the Scripting.Dictionary, and, will write the sorted results in columns C, and, D.

You can change the raw data worksheet name in the macro.

Sample raw data, and, results (not all rows are shown for brevity):


Excel 2007
ABCD
1ACO Service AreaService AreaCount
2AZAK1
3AZAL6
4AZAR10
5ARAZ10
6ARCA32
7AZ, NMCO4
8AZCT14
9AZDC7
10AZDE5
11AZFL47
12AZ, TXGA18
13CAIA9
14CAID2
15CAIL27
16CAIN22
17CAKS9
18CAKY14
19CALA7
20CA, MO, TXMA20
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub GetUniqueCount_V2()
' hiker95, 08/28/2015, ME878969
Dim a As Variant, d As Object, s, o As Variant
Dim lr As Long, i As Long, j As Long
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Cells(.Rows.Count, 1).End(xlUp).Row
  a = .Range("A2:A" & lr)
  .Columns("C:D").ClearContents
  With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For i = LBound(a, 1) To UBound(a, 1)
      If InStr(a(i, 1), ", ") Then
        s = Split(a(i, 1), ", ")
        For j = LBound(s) To UBound(s)
          If .Exists(s(j)) Then
            .Item(s(j)) = .Item(s(j)) + 1
          Else
            .Add s(j), 1
          End If
        Next j
      Else
        If Not .Exists(a(i, 1)) Then
          .Add a(i, 1), 1
        Else
          .Item(a(i, 1)) = .Item(a(i, 1)) + 1
        End If
      End If
    Next i
    o = Application.Transpose(Array(.Keys, .Items))
  End With
  .Range("C1").Resize(, 2).Value = Array("Service Area", "Count")
  .Range("C2").Resize(UBound(o, 1), UBound(o, 2)) = o
  .Range("C2:D" & UBound(o, 1) + 1).Sort key1:=.Range("C2"), order1:=1
  .Columns("C:D").AutoFit
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the GetUniqueCount_V2 macro.
 
Upvote 0
dmshott,

Here is a formula solution:


Excel 2007
ABCD
1ACO Service AreaService AreaCount
2AZAK1
3AZAL6
4AZAR11
5ARAZ10
6ARCA32
7AZ, NMCO5
8AZCT14
9AZDC7
10AZDE5
11AZFL47
12AZ, TXGA18
13CAIA9
14CAID2
15CAIL27
16CAIN22
17CAKS9
18CAKY14
19CALA7
20CA, MO, TXMA20
21CAMD21
22CAME4
23CA, MS, OHMI21
24CAMN4
25CAMO15
26CAMS9
27CAMT3
28CA, FLNC19
29CAND2
30CANE3
31CANH9
32CA, DC, FL, GA, LA, NC, SC, TN, TXNJ26
33CANM3
34CA, PA, TNNV5
35CA, IN, MINY35
36CA, IL, IN, IAOH18
37CAOK7
38OR, WAOR4
39CAPA25
40CAPR2
41IN, OHRI4
42INSC9
43CASD1
44CATN15
45COTX35
46COUT3
47COVA18
48CT, NYVT4
49CTWA4
50CTWI10
51CT, NYWV7
52CTWY2
53CT
Sheet1
Cell Formulas
RangeFormula
D2=COUNTIF(A:A,"*"&C2&"*")


With the state column sorted, range C2:C52, the formula in cell D2, copied down:

=COUNTIF(A:A,"*"&C2&"*")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,499
Messages
6,131,010
Members
449,613
Latest member
MedDash99

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