counting comma delimited in a range

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
201
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
In a single structured value I managed to count using below code
VBA Code:
    Dim LastRow as long, eWan as integer 
    LastRow = 136
    eWan = 3
    Lr0W = 10
    iLOOP = 2
    Do While eWan <= Lr0W 
        Cells(eWan, iLOOP).Formula = "=SUM(COUNTIFS('DREC'!$A$2:$A" & LastRow & ",$A3,'DREC'!$B$2:$B" & LastRow & ",$B$1,'DREC'!$C$2:$C" & LastRow & ",""Hatched""))"       
        eWan = eWan + 1
    Loop

but since I have a loving and very thoughtful supervisor (pun intended) he manage to change it in a comma delimited range value thus the problem arise. anyway I have below a running code for such problem but needed to merge with the above code since I do need to check first the Specie and country before counting the Gender/Sex of the fish/specie
Code:
    Dim iSum As Long
    iSum = Len(Sheets("DREC").Cells(2, 4)) - Len(Replace(Sheets("DREC").Cells(2, 4, "F", ""))

the logic would be if Sheet DREC Specie = Sheet UPDATED Specie AND Sheet DREC Country = Sheet UPDATED Country then count Gender/Sex under such condition, just like the first code but now counting the Gender/Sex in a comma delimited value...

SAMPLE DATA
X ONLINE DUMMY RECORD.xlsx
ABCD
1SPECIECOUNTRYSTATUSGENDER
2American flamingoAfghanistanHatchedM,M,M,F
3AlcidsAfghanistanHatchedF,F,M,F
4Amazon kingfisherAfghanistanHatchedF
5AlbatrossesAfghanistanSoldM
6AlbatrossesAfghanistanSoldF,F
7Accipiter hawksAfghanistanSoldM,F
8Accipiter hawksAfghanistanSoldF
9American dipperAlbaniaHatchedM,M,M
10Accipiter hawksAlbaniaHatchedF,F,F,F
11AlbatrossesAlbaniaSoldM,F
12American flamingoAlgeriaHatchedM
13AlbatrossesAlgeriaSoldM
14AlbatrossesAlgeriaSoldF
15Accipiter hawksAlgeriaSoldF
16American dipperAndorraHatchedM
17AlcidsAndorraHatchedM
18AlbatrossesAndorraSoldM
19AlbatrossesAndorraHatchedF
20Accipiter hawksAndorraSoldF,F
21AlbatrossesAngolaSoldF,M
22Accipiter hawksAngolaSoldM,M,M
23AlbatrossesAntigua and BarbudaSoldM
24AlbatrossesAntigua and BarbudaSoldM
25AlbatrossesAntigua and BarbudaSoldM
26AlbatrossesAntigua and BarbudaSoldF
27AlbatrossesAntigua and BarbudaSoldF,F
28AlbatrossesAntigua and BarbudaSoldF,M
29Accipiter hawksAntigua and BarbudaSoldM,M,M
30Accipiter hawksAntigua and BarbudaSoldM,F
31Accipiter hawksAntigua and BarbudaSoldM
32Accipiter hawksAntigua and BarbudaSoldM
33Accipiter hawksAntigua and BarbudaSoldM,F
34AlbatrossesArgentinaSoldM
35AlbatrossesArgentinaSoldM
36Accipiter hawksArgentinaSoldM
37Accipiter hawksArgentinaSoldM
38American dipperArmeniaHatchedF
39American kestrelArmeniaHatchedF,F
40American white pelicanArmeniaHatchedF,M
41American white pelicanArmeniaHatchedM,M,M
42American white pelicanArmeniaHatchedF,M
43AlbatrossesArmeniaSoldM,M,M
44AlbatrossesArmeniaSoldM,F
45AlbatrossesArmeniaSoldM
46AlbatrossesArmeniaSoldM
47AlbatrossesArmeniaHatchedM,F
48AlbatrossesArmeniaSoldM
49AlbatrossesArmeniaSoldM
50AlcidsArmeniaHatchedM
51Accipiter hawksArmeniaSoldM
52Accipiter hawksArmeniaSoldF
53Accipiter hawksArmeniaSoldF,F
54American dipperAustraliaHatchedF,M
55American white pelicanAustraliaHatchedM,M,M
56Accipiter hawksAustraliaSoldM,F
57Accipiter hawksAustraliaHatchedM
58American dipperAustriaHatchedM
59American dipperAustriaHatchedM,F
60American kestrelAustriaHatchedM
61American white pelicanAustriaHatchedM
62American white pelicanAustriaHatchedM
63AlbatrossesAustriaSoldM
64AlbatrossesAustriaSoldF
65AlbatrossesAustriaSoldF,F
66AlbatrossesAustriaSoldF,M
67AlbatrossesAustriaSoldM,M,M
68AlbatrossesAustriaSoldM,F
69AlbatrossesAustriaSoldM
70AlbatrossesAustriaSoldM
71Accipiter hawksAustriaSoldM,F
72Accipiter hawksAustriaSoldM
73Accipiter hawksAustriaSoldM
74Accipiter hawksAustriaSoldM
75American kestrelAzerbaijanHatchedM
76AlbatrossesAzerbaijanSoldF,M
77American dipperBahamasHatchedM,M,M
78American white pelicanBahrainHatchedM,F
79American dipperBangladeshHatchedM
80AlcidsBangladeshHatchedM
81Accipiter hawksBangladeshHatchedM,F
82American dipperBarbadosHatchedM
83American kestrelBarbadosSoldM
84American kestrelBarbadosHatchedM
85American white pelicanBarbadosHatchedM
86American white pelicanBarbadosHatchedF,M
87AlcidsBarbadosHatchedM,M,M
88Accipiter hawksBarbadosHatchedM,F
89AlbatrossesBarbadosSoldM
90AlbatrossesBarbadosSoldM
91AlbatrossesBarbadosSoldM,F
92Accipiter hawksBarbadosSoldM
93Accipiter hawksBarbadosSoldM
94American dipperBelarusHatchedM
95American white pelicanBelarusHatchedM
96American white pelicanBelarusHatchedF,M
97American white pelicanBelarusHatchedM,M,M
98American white pelicanBelarusHatchedM,F
99American white pelicanBelarusHatchedM
100AlcidsBelarusHatchedM
101Accipiter hawksBelarusHatchedM,F
102AlbatrossesBelarusSoldM
103AlbatrossesBelarusSoldM
104AlbatrossesBelarusSoldM
105Accipiter hawksBelarusSoldM
106Accipiter hawksBelarusSoldF,M
107AlbatrossesBelgiumSoldM,M,M
108AlbatrossesBelgiumSoldM,F
109Accipiter hawksBelgiumSoldM
110Accipiter hawksBelgiumSoldM
111AlcidsBelizeSoldM,F
112AlbatrossesBelizeSoldM
113Accipiter hawksBelizeSoldM
114American white pelicanBeninHatchedM
115American white pelicanBeninHatchedM
116AlcidsBeninHatchedF,M
117Amazon kingfisherBeninSoldM,M,M
118AlbatrossesBeninSoldM,F
119AlbatrossesBeninSoldM
120AlbatrossesBeninSoldM
121AlbatrossesBeninSoldM,F
122Accipiter hawksBeninSoldM
123Accipiter hawksBeninSoldM
124Accipiter hawksBeninSoldM
125Amazon kingfisherBhutanSoldM
126AlbatrossesBoliviaSoldF,M
127AlbatrossesBoliviaSoldM,M,M
128AlbatrossesBoliviaSoldM,F
129Accipiter hawksBoliviaSoldM
130Accipiter hawksBoliviaSoldM
131Accipiter hawksBoliviaSoldM,F
132AlbatrossesBosnia and HerzegovinaSoldM
133AlbatrossesBosnia and HerzegovinaSoldM
134American kestrelBotswanaHatchedM
135AlbatrossesBotswanaSoldM
136AlcidsBotswanaSoldN
DREC


X ONLINE DUMMY RECORD.xlsx
ABCDEFGHIJ
1SPECIEAfghanistanAntigua and BarbudaBarbados
2FEMALEMALETotalFEMALEMALETotalFEMALEMALETotal
3American flamingo
4Alcids
5Amazon kingfisher
6Albatrosses
7Accipiter hawks
8American dipper
9American kestrel
10American white pelican
11Grand Total
UPDATED
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try:
VBA Code:
Option Explicit
Sub test()
Dim dicS As Object, dicC As Object, dicA As Object
Dim lr&, i&, j&, n&, F&, M&, freq&, key, s, id As String, gen As String, cell As Range, rng, arr()
Set dicS = CreateObject("Scripting.dictionary")
Set dicC = CreateObject("Scripting.dictionary")
Set dicA = CreateObject("Scripting.dictionary")
Worksheets("DREC").Activate
lr = Cells(Rows.Count, "A").End(xlUp).Row
rng = Range("A2:D" & lr).Value
    For i = 1 To lr - 1
        M = 0: F = 0: gen = ""
        s = Split(rng(i, 4), ",")
        id = rng(i, 1) & "|" & rng(i, 2)
        If Not dicA.exists(id) And rng(i, 3) = "Hatched" Then
            For n = 1 To Len(rng(i, 4))
                Select Case Mid(rng(i, 4), n, 1)
                    Case "F"
                        F = F + 1
                    Case "M"
                        M = M + 1
                End Select
            Next
            gen = M & "," & F & "," & M + F
            dicA.Add id, gen
        End If
        If Not dicS.exists(rng(i, 1)) And rng(i, 3) = "Hatched" Then dicS.Add rng(i, 1), ""
        If Not dicC.exists(rng(i, 2)) And rng(i, 3) = "Hatched" Then dicC.Add rng(i, 2), ""
    Next
ReDim arr(1 To dicS.Count + 1, 1 To dicC.Count * 3)
Worksheets("UPDATED").Activate
Range("A3").Resize(dicS.Count, 1) = WorksheetFunction.Transpose(dicS.keys)
    For i = 1 To dicS.Count
        For j = 1 To dicC.Count * 3
            freq = Evaluate("Mod(" & j - 1 & ",3" & ")")
            For Each key In dicA.keys
                If key Like dicS.keys()(i - 1) & "|" & dicC.keys()(Int((j - 1) / 3)) Then
                    arr(i, j) = Split(dicA(key), ",")(freq)
                End If
            Next
            If freq = 1 Then
                Cells(1, j).Value = dicC.keys()(Int((j - 1) / 3))
                Cells(2, j).Resize(1, 3).Value = Array("FEMALE", "MALE", "Total")
            End If
        Next
    Next
Range("B3").Resize(i, j - 1) = arr
End Sub
 
Upvote 0
Try:
VBA Code:
Option Explicit
Sub test()
Dim dicS As Object, dicC As Object, dicA As Object
Dim lr&, i&, j&, n&, F&, M&, freq&, key, s, id As String, gen As String, cell As Range, rng, arr()
Set dicS = CreateObject("Scripting.dictionary")
Set dicC = CreateObject("Scripting.dictionary")
Set dicA = CreateObject("Scripting.dictionary")
Worksheets("DREC").Activate
lr = Cells(Rows.Count, "A").End(xlUp).Row
rng = Range("A2:D" & lr).Value
    For i = 1 To lr - 1
        M = 0: F = 0: gen = ""
        s = Split(rng(i, 4), ",")
        id = rng(i, 1) & "|" & rng(i, 2)
        If Not dicA.exists(id) And rng(i, 3) = "Hatched" Then
            For n = 1 To Len(rng(i, 4))
                Select Case Mid(rng(i, 4), n, 1)
                    Case "F"
                        F = F + 1
                    Case "M"
                        M = M + 1
                End Select
            Next
            gen = M & "," & F & "," & M + F
            dicA.Add id, gen
        End If
        If Not dicS.exists(rng(i, 1)) And rng(i, 3) = "Hatched" Then dicS.Add rng(i, 1), ""
        If Not dicC.exists(rng(i, 2)) And rng(i, 3) = "Hatched" Then dicC.Add rng(i, 2), ""
    Next
ReDim arr(1 To dicS.Count + 1, 1 To dicC.Count * 3)
Worksheets("UPDATED").Activate
Range("A3").Resize(dicS.Count, 1) = WorksheetFunction.Transpose(dicS.keys)
    For i = 1 To dicS.Count
        For j = 1 To dicC.Count * 3
            freq = Evaluate("Mod(" & j - 1 & ",3" & ")")
            For Each key In dicA.keys
                If key Like dicS.keys()(i - 1) & "|" & dicC.keys()(Int((j - 1) / 3)) Then
                    arr(i, j) = Split(dicA(key), ",")(freq)
                End If
            Next
            If freq = 1 Then
                Cells(1, j).Value = dicC.keys()(Int((j - 1) / 3))
                Cells(2, j).Resize(1, 3).Value = Array("FEMALE", "MALE", "Total")
            End If
        Next
    Next
Range("B3").Resize(i, j - 1) = arr
End Sub
thanks for the time mate,

running the code seems to work except it's not accurate
ie.
Afghanistan should be 6 Male and 9 Female for a total of 15
Albania should 4 Male and 5 Female total 9
 
Upvote 0
According to your initial formula:
Code:
=SUM(COUNTIFS('DREC'!$A$2:$A" & LastRow & ",$A3,'DREC'!$B$2:$B" & LastRow & ",$B$1,'DREC'!$C$2:$C" & LastRow & ",""Hatched"")
With "Hatched" is 1 of condition to count.
Without "Hatched", Afghanistan Male is 6
With "Hatched" it is 4 (As my code)
What 'd your condition, with or without "Hatched"?
 
Upvote 0
the logic would be if Sheet DREC Specie = Sheet UPDATED Specie AND Sheet DREC Country = Sheet UPDATED Country then count Gender/Sex under such condition, just like the first code but now counting the Gender/Sex in a comma delimited value...
sorry mate its my fault for not making it clear :)
I'm not counting the hatched anymore but the Gender/Sex as what stated above...
 
Upvote 0
There were 3 of
Code:
And rng(i, 3) = "Hatched"
in my code. Just delete and try again.
 
Upvote 0
I personally think you would be better served by a pivot table. It would give you options in how you analyse the data eg.

20220524 VBA Sum Comma Delimited airforceone v03.xlsm
ABCDEFGHIJKLMNO
1SPECIECOUNTRYSTATUSGENDERFemaleMale
2American flamingoAfghanistanHatchedM,M,M,F13
3AlcidsAfghanistanHatchedF,F,M,F31COUNTRYValues
4Amazon kingfisherAfghanistanHatchedF10AfghanistanAlbania
5AlbatrossesAfghanistanSoldM01SPECIE Female Male Gender Total Female Male Gender Total
6AlbatrossesAfghanistanSoldF,F20Accipiter hawks213404
7Accipiter hawksAfghanistanSoldM,F11Albatrosses213112
8Accipiter hawksAfghanistanSoldF10Alcids3140
9American dipperAlbaniaHatchedM,M,M03Amazon kingfisher1010
10Accipiter hawksAlbaniaHatchedF,F,F,F40American dipper0033
11AlbatrossesAlbaniaSoldM,F11American flamingo1340
12American flamingoAlgeriaHatchedM01American kestrel00
13AlbatrossesAlgeriaSoldM01American white pelican00
14AlbatrossesAlgeriaSoldF10Grand Total9615549
15Accipiter hawksAlgeriaSoldF10
16American dipperAndorraHatchedM01
17AlcidsAndorraHatchedM01
18AlbatrossesAndorraSoldM01
DREC
Cell Formulas
RangeFormula
E2:F18E2=LEN($D2)-LEN(SUBSTITUTE($D2,LEFT(E$1,1),""))
 
Upvote 0
If you really want to use VBA, I have made some modifications to @bebo021999's code, see if that works for you.

VBA Code:
Sub test_v02()
    Dim dicS As Object, dicC As Object, dicA As Object
    Dim lr&, i&, j&, n&, F&, M&, freq&, key, s, id As String, gen As String, cell As Range, rng, arr()
    Dim sDic As Variant
    Set dicS = CreateObject("Scripting.dictionary")
    Set dicC = CreateObject("Scripting.dictionary")
    Set dicA = CreateObject("Scripting.dictionary")
    Worksheets("DREC").Activate
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    rng = Range("A2:D" & lr).Value
    For i = 1 To lr - 1
        s = Split(rng(i, 4), ",")
        id = rng(i, 1) & "|" & rng(i, 2)
        If dicA.exists(id) Then
            sDic = Split(dicA(id), ",")
            F = sDic(0): M = sDic(1): gen = sDic(2)
        Else
            F = 0: M = 0: gen = ""
        End If
        For n = 1 To Len(rng(i, 4))
            Select Case Mid(rng(i, 4), n, 1)
                Case "F"
                    F = F + 1
                Case "M"
                    M = M + 1
            End Select
        Next
            gen = F & "," & M & "," & M + F         ' XXX Swapped M & F
            dicA(id) = gen
        If Not dicS.exists(rng(i, 1)) Then dicS.Add rng(i, 1), ""
        If Not dicC.exists(rng(i, 2)) Then dicC.Add rng(i, 2), ""
    Next
    ReDim arr(1 To dicS.Count + 1, 1 To dicC.Count * 3)
    Worksheets("UPDATED").Activate
    Range("A3").Resize(dicS.Count, 1) = WorksheetFunction.Transpose(dicS.keys)
    For i = 1 To dicS.Count
        For j = 1 To dicC.Count * 3
            freq = Evaluate("Mod(" & j - 1 & ",3" & ")")
            For Each key In dicA.keys
                If key Like dicS.keys()(i - 1) & "|" & dicC.keys()(Int((j - 1) / 3)) Then
                    arr(i, j) = Split(dicA(key), ",")(freq)
                End If
            Next
            If freq = 1 Then
                Cells(1, j).Value = dicC.keys()(Int((j - 1) / 3))
                Cells(2, j).Resize(1, 3).Value = Array("FEMALE", "MALE", "Total")
            End If
        Next
    Next
    Range("B3").Resize(i, j - 1) = arr
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,718
Messages
6,174,077
Members
452,542
Latest member
Bricklin

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