countifs not working in macro

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
201
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Sorry but I'm back again.... I hope everyone is well and good...

anyway I have 2 Sheet (Dummy and Updated) from Sheet Dummy my code would supposedly insert my Sum and CountIfs formula in the designated cell in my Sheet Updated, although it runs but the problem is the total value does not represent the actual figure from the Sheet Dummy, while the same formula inserted manually as the sample provided in Sheet Updated Column E to J works like a charmed! below is the sample table and code intended for your violent analysis :) and hopefully the right path to the correct code. Oh btw, I have 3 Variation of Formula in my VBA Code

DUMMY ZERO.xlsx
ABC
1SPECIECOUNTRYSTATUS
2American flamingoAfghanistanHatched
3AlcidsAfghanistanHatched
4Amazon kingfisherAfghanistanHatched
5AlbatrossesAfghanistanSold
6AlbatrossesAfghanistanSold
7Accipiter hawksAfghanistanSold
8Accipiter hawksAfghanistanSold
9American dipperAlbaniaHatched
10Accipiter hawksAlbaniaHatched
11AlbatrossesAlbaniaSold
12American flamingoAlgeriaHatched
13AlbatrossesAlgeriaSold
14AlbatrossesAlgeriaSold
15Accipiter hawksAlgeriaSold
16American dipperAndorraHatched
17AlcidsAndorraHatched
18AlbatrossesAndorraSold
19AlbatrossesAndorraHatched
20Accipiter hawksAndorraSold
21AlbatrossesAngolaSold
22Accipiter hawksAngolaSold
23AlbatrossesAntigua and BarbudaSold
24AlbatrossesAntigua and BarbudaSold
25AlbatrossesAntigua and BarbudaSold
26AlbatrossesAntigua and BarbudaSold
27AlbatrossesAntigua and BarbudaSold
28AlbatrossesAntigua and BarbudaSold
29Accipiter hawksAntigua and BarbudaSold
30Accipiter hawksAntigua and BarbudaSold
31Accipiter hawksAntigua and BarbudaSold
32Accipiter hawksAntigua and BarbudaSold
33Accipiter hawksAntigua and BarbudaSold
34AlbatrossesArgentinaSold
35AlbatrossesArgentinaSold
36Accipiter hawksArgentinaSold
37Accipiter hawksArgentinaSold
38American dipperArmeniaHatched
39American kestrelArmeniaHatched
40American white pelicanArmeniaHatched
41American white pelicanArmeniaHatched
42American white pelicanArmeniaHatched
43AlbatrossesArmeniaSold
44AlbatrossesArmeniaSold
45AlbatrossesArmeniaSold
46AlbatrossesArmeniaSold
47AlbatrossesArmeniaHatched
48AlbatrossesArmeniaSold
49AlbatrossesArmeniaSold
50AlcidsArmeniaHatched
51Accipiter hawksArmeniaSold
52Accipiter hawksArmeniaSold
53Accipiter hawksArmeniaSold
54American dipperAustraliaHatched
55American white pelicanAustraliaHatched
56Accipiter hawksAustraliaSold
57Accipiter hawksAustraliaHatched
58American dipperAustriaHatched
59American dipperAustriaHatched
60American kestrelAustriaHatched
61American white pelicanAustriaHatched
62American white pelicanAustriaHatched
63AlbatrossesAustriaSold
64AlbatrossesAustriaSold
65AlbatrossesAustriaSold
66AlbatrossesAustriaSold
67AlbatrossesAustriaSold
68AlbatrossesAustriaSold
69AlbatrossesAustriaSold
70AlbatrossesAustriaSold
71Accipiter hawksAustriaSold
72Accipiter hawksAustriaSold
73Accipiter hawksAustriaSold
74Accipiter hawksAustriaSold
75American kestrelAzerbaijanHatched
76AlbatrossesAzerbaijanSold
77American dipperBahamasHatched
78American white pelicanBahrainHatched
79American dipperBangladeshHatched
80AlcidsBangladeshHatched
81Accipiter hawksBangladeshHatched
82American dipperBarbadosHatched
83American kestrelBarbadosSold
84American kestrelBarbadosHatched
85American white pelicanBarbadosHatched
86American white pelicanBarbadosHatched
87AlcidsBarbadosHatched
88Accipiter hawksBarbadosHatched
89AlbatrossesBarbadosSold
90AlbatrossesBarbadosSold
91AlbatrossesBarbadosSold
92Accipiter hawksBarbadosSold
93Accipiter hawksBarbadosSold
94American dipperBelarusHatched
95American white pelicanBelarusHatched
96American white pelicanBelarusHatched
97American white pelicanBelarusHatched
98American white pelicanBelarusHatched
99American white pelicanBelarusHatched
100AlcidsBelarusHatched
101Accipiter hawksBelarusHatched
102AlbatrossesBelarusSold
103AlbatrossesBelarusSold
104AlbatrossesBelarusSold
105Accipiter hawksBelarusSold
106Accipiter hawksBelarusSold
107AlbatrossesBelgiumSold
108AlbatrossesBelgiumSold
109Accipiter hawksBelgiumSold
110Accipiter hawksBelgiumSold
111AlcidsBelizeSold
112AlbatrossesBelizeSold
113Accipiter hawksBelizeSold
114American white pelicanBeninHatched
115American white pelicanBeninHatched
116AlcidsBeninHatched
117Amazon kingfisherBeninSold
118AlbatrossesBeninSold
119AlbatrossesBeninSold
120AlbatrossesBeninSold
121AlbatrossesBeninSold
122Accipiter hawksBeninSold
123Accipiter hawksBeninSold
124Accipiter hawksBeninSold
125Amazon kingfisherBhutanSold
126AlbatrossesBoliviaSold
127AlbatrossesBoliviaSold
128AlbatrossesBoliviaSold
129Accipiter hawksBoliviaSold
130Accipiter hawksBoliviaSold
131Accipiter hawksBoliviaSold
132AlbatrossesBosnia and HerzegovinaSold
133AlbatrossesBosnia and HerzegovinaSold
134American kestrelBotswanaHatched
135AlbatrossesBotswanaSold
136AlcidsBotswanaSold
Dummy


DUMMY ZERO.xlsx
ABCDEFGHIJ
1SPECIEAfghanistanAntigua and BarbudaBarbados
2HatchedSoldTotalHatchedSoldTotalHatchedSoldTotal
3American flamingo0000000
4Alcids0000101
5Amazon kingfisher0000000
6Albatrosses0066033
7Accipiter hawks0055123
8American dipper0000101
9American kestrel0000112
10American white pelican0000202
11Grand Total000011116612
12VBA EncodedManually Encoded Formula / NON - VBA
UPDATED
Cell Formulas
RangeFormula
D3:D10,J3:J10,G3:G10D3=SUM(B3:C3)
E3:E10E3=SUM(COUNTIFS(Dummy!A2:A136,$A3,Dummy!B2:B136,$E$1,Dummy!C2:C136,"Hatched"))
F3:F10F3=SUM(COUNTIFS(Dummy!A2:A136,$A3,Dummy!B2:B136,$E$1,Dummy!C2:C136,"Sold"))
H3:H10H3=SUM(COUNTIFS(Dummy!A2:A136,$A3,Dummy!B2:B136,$H$1,Dummy!C2:C136,"Hatched"))
I3:I10I3=SUM(COUNTIFS(Dummy!A2:A136,$A3,Dummy!B2:B136,$H$1,Dummy!C2:C136,"Sold"))
B11:J11B11=SUM(B3:B10)


VBA Code:
    Dim MyRangeA As Range, MyRangeB As Range, MyRangeC As Range
    Dim LastRow as long, eWan as integer
  
    LastRow = 136
    eWan = 3
    Lr0W = 10
  
'   SPECIE
    Set MyRangeA = Sheets("Dummy").Range("A2:A" & LastRow)
'   COUNTRY
    Set MyRangeB = Sheets("Dummy").Range("B2:B" & LastRow)
'   STATUS
    Set MyRangeC = Sheets("Dummy").Range("C2:C" & LastRow)

    iLOOP = 2
    Do While eWan <= Lr0W
  
    '    Formula A - 1
        Cells(eWan, iLOOP).Formula = "=SUM(COUNTIFS('Dummy'!$A$2:$A" & LastRow & ",$A3,'Dummy'!$B$2:$B" & LastRow & ",$B$1,'Dummy'!$C$2:$C" & LastRow & ",""Hatched""))"

    '    Formula A - 2
        Cells(eWan, iLOOP).Formula = "=SUM(COUNTIFS(Dummy!$A$2:$A" & LastRow & ",$A3,Dummy!$B$2:$B" & LastRow & ",$B$1,Dummy!$C$2:$C" & LastRow & ",""Hatched""))"     

    '    Formula B - 1
        Cells(eWan, iLOOP + 1).Formula = WorksheetFunction.Sum(Application.WorksheetFunction.CountIfs(MyRangeA, "$A3", MyRangeB, "$B$1", MyRangeC, "Hatched"))
          
        eWan = eWan + 1
    Loop
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Is this red text correct?
Cells(eWan, iLOOP).Formula = "=SUM(COUNTIFS('Dummy'!$A$2:$A" & LastRow & ",$A3,'Dummy'!$B$2:$B" & LastRow & ",$B$1,'Dummy'!$C$2:$C" & LastRow & ",""Hatched""))"
 
Upvote 0
Is this red text correct?
Cells(eWan, iLOOP).Formula = "=SUM(COUNTIFS('Dummy'!$A$2:$A" & LastRow & ",$A3,'Dummy'!$B$2:$B" & LastRow & ",$B$1,'Dummy'!$C$2:$C" & LastRow & ",""Hatched""))"
that's correct mate, it reference to the country that specie belongs to...
You may or may not used my formula. What the code do is, Checks the specie under specific country with either "Hatched" or "Sold" status.

$A3 = Specie
$B$1 = Country

in my example I just Merge the Country cell address for easy reading...
 
Upvote 0
SOLVED!
the villain in the code is $A3 it doesn't change at all :) but all is good now! I just have to increment my row address!

VBA Code:
Cells(eWan, iLOOP).Formula = "=SUM(COUNTIFS('Dummy'!$A$2:$A" & LastRow & ",$A3,'Dummy'!$B$2:$B" & LastRow & ",$B$1,'Dummy'!$C$2:$C" & LastRow & ",""Hatched""))"
 
Upvote 0
Solution
FYI, you don't need the SUM there as your COUNTIFS is only returning one result anyway. You also don't need a loop really.
 
Upvote 0
FYI, you don't need the SUM there as your COUNTIFS is only returning one result anyway. You also don't need a loop really.
hmm, very interesting mate and I'm interested :)
would you be kind enough to recode it? or at least push me to a more efficient coding?
it might make my code a bit faster (if you have time to spare)
 
Upvote 0
Like this:

Code:
Range(Cells(eWan, iLOOP), Cells(Lr0W, iLOOP)).Formula = "=COUNTIFS('Dummy'!$A$2:$A" & LastRow & ",$A3,'Dummy'!$B$2:$B" & LastRow & ",$B$1,'Dummy'!$C$2:$C" & LastRow & ",""Hatched"")"
 
Upvote 0
Like this:

Code:
Range(Cells(eWan, iLOOP), Cells(Lr0W, iLOOP)).Formula = "=COUNTIFS('Dummy'!$A$2:$A" & LastRow & ",$A3,'Dummy'!$B$2:$B" & LastRow & ",$B$1,'Dummy'!$C$2:$C" & LastRow & ",""Hatched"")"
thanks mate your code is much like Lightning McQueen! fast and accurate
but my columns and rows are dynamic, that may range from 35 to 165 columns and 15 up to 96 rows per month
inserting formula in that scenario would also entails using looping function.... I think for now I'll just stick with my looping method :) but thanks mate I'll definitely used it in small sample data, cheers.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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