Counting problem

sharshra

Active Member
Joined
Mar 20, 2013
Messages
404
Office Version
  1. 365
I have a table with one of the field having comma separated values. I want to count for each value in that field how many items are there. I´m sure there must be a simple way to do this.

For example, consider the table below. It has name of sales person & regions they cover. I would like to have a formula to list down how many sales persons are there for each region. Note that the actual table has thousands of values. I have taken just 4 rows for illustration.

Table -
Book1
CD
3NameRegion
4JamesParis, London, Madrid
5RogerLondon, Zurich
6ThomasZurich, Berlin, Paris, Warsaw
7AnthonyParis, Madrid
Sheet1


Output required -
Book1
EF
8RegionNo. of sales persons
9Paris3
10London2
11Madrid2
12Zurich2
13Berlin1
14Warsaw1
Sheet1
 
not sure why you quoted my entire post in your reply
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Thanks, etaf. Textjoin function is returning an error with error message saying that the text is too long. As I mentioned before, table has thousands of rows with the region field containing tens of regions.
 
Upvote 0
@Fluff - that formula seems to crash my version of excel

Version 365 for OSX
16.85 (24051214)

i often like to try out other solutions and see how they work, just to learn

i'm not sure i have a solution ....... other than maybe VBA , which i do not provide solutions for
and the textjoin / textsplit may have array limits or row/columns limit - dont know

1000's or rows , with 10's of regions does not sound to many - BUT as you get an error clearly is an issue
 
Last edited:
Upvote 0
Here's a VBA option.
VBA Code:
Sub StoreAndCountCities()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim dataRange As Range
    Dim dataArray() As Variant
    Dim cityDict As Object
    Dim cellValue As Variant
    Dim cities As Variant
    Dim city As Variant
    Dim i As Long
    Dim outputRow As Long
 
    Set ws = ThisWorkbook.Sheets("Sheet3") 'change sheet name as needed
    lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
    Set dataRange = ws.Range("D4:D" & lastRow) 'Region data starts in D4
    dataArray = dataRange.Value
 
    Set cityDict = CreateObject("Scripting.Dictionary")
 
    For i = LBound(dataArray, 1) To UBound(dataArray, 1)
        cellValue = dataArray(i, 1)
        cities = Split(cellValue, ",")
        For Each city In cities
            city = Trim(city)
            If cityDict.Exists(city) Then
                cityDict(city) = cityDict(city) + 1
            Else
                cityDict.Add city, 1
            End If
        Next city
    Next i
  
    'Output
    outputRow = 8
    Dim cityKey As Variant
    For Each cityKey In cityDict.Keys
        ws.Cells(outputRow, "E").Value = cityKey
        ws.Cells(outputRow, "F").Value = cityDict(cityKey)
        outputRow = outputRow + 1
    Next cityKey
  
End Sub
 
Last edited:
Upvote 0
Wow Fluff :) I tried your solution on the sample table & it works perfectly well :biggrin: However, I couldn´t understand how it works :unsure: but it is doing the job (y) I´m wondering how can you create such complex formula in no time!!!!! Kudos to you :love: I have to spend some time to decipher your formula & understand it. Do you suggest any good references?

I noticed the following while applying the formula to the actual table. Can you please advise how these can be resolved?

1. The formula works perfectly till it encounter a blank row. When it hits a blank row, it throws an error.

2. The formula works when it is in the same worksheet as the table. If the table & formula are in different worksheets & when we give the cell reference in another worksheet, it opens up a dialog box shown below :rolleyes: Not sure why this comes up. It is not a blocker though. I can still use it in the same sheet, but curious to know why this happens.
1716927215386.png
 
Upvote 0
With few modifications to the VBA above, the UDF can take in a chosen delimiter, and multiple ranges.
Book1
CDEFGHI
1
2
3NameRegion1Region2Paris3
4JamesParis, London, MadridNew York,Los Angeles,ChicagoLondon2
5RogerLondon, ZurichHouston,Phoenix,Philadelphia,San AntonioMadrid2
6ThomasZurich, Berlin, Paris, WarsawSan Diego,Dallas,San Jose,AustinZurich2
7AnthonyParis, MadridJacksonville,San Francisco,Indianapolis,Columbus,Fort WorthBerlin1
8Charlotte,Seattle,Denver,El PasoWarsaw1
9New York1
10Los Angeles1
11Chicago1
12Houston1
13Phoenix1
14Philadelphia1
15San Antonio1
16San Diego1
17Dallas1
18San Jose1
19Austin1
20Jacksonville1
21San Francisco1
22Indianapolis1
23Columbus1
24Fort Worth1
25Charlotte1
26Seattle1
27Denver1
28El Paso1
Sheet3
Cell Formulas
RangeFormula
H3:I28H3=CountDelimText(",",D4:D7,F4:F8)
Dynamic array formulas.

VBA Code:
Function CountDelimText(delim As String, ParamArray dataRanges() As Variant) As Variant
    Dim cityDict As Object
    Dim cellValue As Variant
    Dim cities As Variant
    Dim city As Variant
    Dim i As Long, j As Long
    Dim dataArray() As Variant
    Dim resultArray() As Variant
    Dim cityKey As Variant
    Dim rowCount As Long
    Dim singleRange As Range
 
    Set cityDict = CreateObject("Scripting.Dictionary")
 
    For i = LBound(dataRanges) To UBound(dataRanges)
        Set singleRange = dataRanges(i)
        dataArray = singleRange.Value
        For j = LBound(dataArray, 1) To UBound(dataArray, 1)
            cellValue = dataArray(j, 1)
            cities = Split(cellValue, delim)
            For Each city In cities
                city = Trim(city)
                If cityDict.Exists(city) Then
                    cityDict(city) = cityDict(city) + 1
                Else
                    cityDict.Add city, 1
                End If
            Next city
        Next j
    Next i
    rowCount = cityDict.Count
    ReDim resultArray(1 To rowCount, 1 To 2)
    i = 1
    For Each cityKey In cityDict.Keys
        resultArray(i, 1) = cityKey
        resultArray(i, 2) = cityDict(cityKey)
        i = i + 1
    Next cityKey
 
    CountDelimText = resultArray
End Function
 
Upvote 0
@Fluff - that formula seems to crash my version of excel
That happens if there are blank rows in the range. This will solve that
Excel Formula:
=LET(a,UNIQUE(DROP(REDUCE("",FILTER(D4:D1000,D4:D1000<>""),LAMBDA(x,y,VSTACK(x,TRIM(TEXTSPLIT(y,,","))))),1)),HSTACK(a,COUNTIFS(D:D,"*"&a&"*")))
 
Upvote 0
Solution
If the table & formula are in different worksheets & when we give the cell reference in another worksheet, it opens up a dialog box shown below :rolleyes:
That sounds as though you have given the wrong sheet name in the formula.
The formula works perfectly till it encounter a blank row. When it hits a blank row, it throws an error.
That is fixed with the formula above.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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