counting comma delimited in a range

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
177
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
 
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
works like a charm! @Alex Blakenburg I'm really newbie at dictionary and the like, would you be kind enough to at least show me some good reading materials related to take on how to master dictionary usage...
@bebo021999 thank also mate...
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Thanks for letting us know. @bebo021999 did most of the heavy lifting.
I like Paul Kelly's stuff (Excel Macro Mastery) and you might want to just start with a look at his youtube video below.
(you can then see if you want to watch the other 3 in the series only the 4th is longe than 10 mins)

 
Upvote 0
Thanks for letting us know. @bebo021999 did most of the heavy lifting.
I like Paul Kelly's stuff (Excel Macro Mastery) and you might want to just start with a look at his youtube video below.
(you can then see if you want to watch the other 3 in the series only the 4th is longe than 10 mins)

will do mate thanks again...
 
Upvote 0

Forum statistics

Threads
1,215,332
Messages
6,124,313
Members
449,152
Latest member
PressEscape

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