Dynamic Countifs not counting

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
214
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Good day mate...
got the code from here Countifs Loop and been trying with my code but does not seem to work, does not give up any error but all result is zero (0).
my manual piece of code works but when applying the dynamic doesn't give the correct result

VBA Code:
Sub TerminateCount()
'https://www.mrexcel.com/board/threads/vba-countifs-with-loop.1086079/    
    Dim wsSource As Worksheet, wsTarget As Worksheet
    Dim ctrX As Double, LRowSource As Double, LRowTarget As Double    
    Set wsSource = Sheets("SOURCE")
    Set wsTarget = Sheets("Tally")    
    LRowSource = wsSource.Range("A" & Rows.count).End(xlUp).Row
    LRowTarget = wsTarget.Range("A" & Rows.count).End(xlUp).Row    
    For ctrX = 2 To LRowTarget 
        '=COUNTIFS(Tally!$B$2:$B$34,$A2,Tally!$D$2:$D$34,$G$1)
        wsTarget.Range("G2:G" & LRowTarget).Formula = WorksheetFunction.CountIfs( _
            wsSource.Range("A2:A" & LRowSource), wsTarget.Cells(ctrX, "A"), wsSource.Range("C2:C" & LRowSource), "Male")
        '=COUNTIFS(Tally!$B$2:$B$34,$A2,Tally!$C$2:$C$34,"<11")
        wsTarget.Range("G2:G" & LRowTarget).Formula = WorksheetFunction.CountIfs( _
            wsSource.Range("A2:A" & LRowSource), wsTarget.Cells(ctrX, "A"), wsSource.Range("B2:B" & LRowSource), "<11")
    Next    
'    Debug.Print "LRowSource : " & LRowSource & " / LRowTarget : " & LRowTarget
End Sub

ANIMALAGESEX
BAT55Female
BAT36Male
BAT36Male
BAT49Male
BIRD42Male
BIRD47Male
BIRD61Male
BIRD63Male
BIRD28Male
BIRD31Male
BIRD35Male
BIRD24Male
BIRD45Male
BIRD39Female
BIRD8Male
BIRD24Female
BIRD48Male
BIRD66Male
BIRD54Female
BIRD66Male
BIRD17Male
BIRD18Male
BIRD22Male
BIRD2Male
BIRD46Female
BIRD43Female
BIRD18Male
BIRD49Male
BIRD15Female
BIRD11Female
BIRD24Female
BIRD35Male
BIRD74Male
BIRD40Male
BIRD20Male
BIRD58Male
BIRD51Female
BIRD21Female
BIRD28Female
BIRD29Male
BIRD52Male
BIRD29Female
BIRD30Male
BIRD39Male
BIRD19Female
BIRD21Male
BIRD42Male
BIRD19Female
BIRD33Male
BIRD7Male
BIRD34Male
BIRD25Male
BIRD66Male
BIRD41Male
BIRD33Male
BIRD64Female
BIRD37Male
BIRD29Male
BIRD28Female
BIRD20Male
BIRD46Female
BIRD34Male
BIRD41Male
BIRD78Female
BIRD43Male
BIRD7Male
BIRD42Female
BIRD41Female
BIRD21Male
BIRD29Male
BIRD62Male
BIRD59Male
BIRD22Male
BIRD28Male
BIRD50Male
BIRD29Male
BIRD22Male
BIRD39Male
BIRD35Female
BIRD46Male
BIRD7Male
BIRD35Male
BIRD18Female
BIRD65Female
BIRD18Male
BIRD58Female
BIRD19Female
BIRD57Male
BIRD3Male
BIRD25Male
BIRD31Male
BIRD3Male
BIRD45Female
BIRD42Female
BIRD17Male
BIRD54Male
BIRD12Female
BIRD32Female
BIRD25Female
BIRD26Male
BIRD26Female
BIRD46Female
BIRD33Male
BIRD43Male
BIRD36Female
BIRD32Male
BIRD34Female
BIRD28Male
BIRD23Female
BIRD47Male
BIRD28Female
BIRD10 months oldFemale
BIRD9Female
BIRD8Male
BIRD54Male
BIRD36Male
BIRD24Male
BIRD14Male
BIRD15Male
BIRD10Female
BIRD8Female
BIRD32Female
BIRD29Male
BIRD11Male
BIRD17Male
BIRD35Male
BIRD31Female
BIRD52Male
BIRD53Female
BIRD5Male
BIRD44Male
BIRD21Male
BIRD24Male
BIRD56Male
BIRD14Male
BIRD0Female
BIRD27Female
BIRD0Female
BIRD67Male
BIRD46Male
BIRD60Male
BIRD36Male
BIRD31Female
BIRD51Male
BIRD44Male
BIRD57Male
BIRD50Male
BIRD56Female
BIRD87Female
BIRD75Male
BIRD71Female
BIRD21Male
BIRD68Male
BIRD21Male
BIRD22Male
BIRD56Male
BIRD52Male
BIRD54Female
BIRD31Female
BIRD24Male
BIRD24Male
BIRD27Male
BIRD21Male
BIRD34Male
BIRD46Male
BIRD46Female
BIRD19Female
BIRD45Female
BIRD29Male
BIRD25Male
BIRD41Male
BIRD13Female
BIRD24Female
BIRD17Male
BIRD25Male
BIRD21Female
BIRD11Female
BIRD39Female
BIRD35Male
BIRD70Female
BIRD2Female
BIRD37Female
BIRD31Male
BIRD28Female
BIRD16Female
BIRD82Male
BIRD20Male
BIRD32Female
BIRD42Male
BIRD39Female
BIRD56Male
BIRD21Male
BIRD66Male
BIRD53Female
BIRD28Male
BIRD26Male
BIRD27Male
BIRD38Male
BIRD19Male
BIRD21Male
BIRD46Male
BIRD75Female
CAT74Female
CAT49Male
CAT18Male
CAT18Male
CAT43Female
CAT22Male
CAT15Female
CAT2Male
CAT46Female
CAT69Female
CAT21Male
DOG42Male
DOG47Male
DOG61Male
DOG45Male
DOG67Male
DOG63Male
DOG31Male
DOG28Male
DOG35Male
DOG24Male
DOG45Male
DOG39Female
DOG8Male
DOG48Male
DOG24Female
DOG66Male
DOG54Female
DOG17Male
DOG66Male
DOG50Male
DOG36Male
DOG49Male
DOG2Male
DOG46Female
DOG22Male
DOG15Female
DOG43Female
DOG18Male
DOG18Male
DOG11Female
DOG35Male
DOG24Female
DOG74Male
DOG48Male
DOG40Male
DOG44Male
DOG34Male
DOG58Male
DOG20Male
DOG44Male
DOG21Female
DOG51Female
DOG28Female
DOG52Male
DOG29Male
DOG29Female
DOG30Male
DOG39Male
DOG58Male
DOG69Male
DOG32Male
DOG19Female
DOG21Male
DOG42Male
DOG19Female
DOG33Male
DOG48Female
DOG7Male
DOG34Male
DOG25Male
DOG66Male
DOG36Male
DOG33Male
DOG60Male
DOG41Male
DOG33Male
DOG64Female
DOG24Female
DOG37Male
DOG20Male
DOG29Male
DOG28Female
DOG46Female
DOG34Male
DOG51Male
DOG48Male
DOG41Male
DOG78Female
DOG43Male
DOG41Female
DOG7Male
DOG42Female
DOG32Female
DOG21Male
DOG29Male
DOG46Male
DOG62Male
DOG59Male
DOG28Male
DOG22Male
DOG50Male
DOG29Male
DOG22Male
DOG39Male
DOG35Female
DOG39Male
DOG46Male
DOG7Male
DOG46Male
DOG65Female
DOG34Female
DOG28Male
DOG35Male
DOG52Male
DOG34Male
DOG18Female
DOG18Male
DOG65Female
DOG29Male
DOG57Male
DOG3Male
DOG58Female
DOG19Female
DOG25Male
DOG31Male
DOG42Female
DOG45Female
DOG3Male
DOG17Male
DOG54Male
DOG40Male
DOG32Female
DOG25Female
DOG12Female
DOG43Male
DOG44Male
DOG26Male
DOG26Female
DOG26Male
DOG33Male
DOG46Female
DOG32Male
DOG43Male
DOG36Female
DOG34Female
DOG28Male
DOG23Female
DOG48Female
DOG47Male
DOG54Male
DOG8Male
DOG9Female
DOG28Female
DOG10 months oldFemale
DOG24Male
DOG36Male
DOG15Male
DOG10Female
DOG14Male
DOG42Female
DOG34Male
DOG30Male
DOG32Female
DOG11Male
DOG29Male
DOG8Female
DOG17Male
DOG35Male
DOG5Male
DOG31Female
DOG52Male
DOG53Female
DOG44Male
DOG21Male
DOG24Male
DOG29Male
DOG56Male
DOG39Male
DOG0Female
DOG0Female
DOG27Female
DOG14Male
DOG67Male
DOG46Male
DOG47Male
DOG36Male
DOG31Female
DOG37Male
DOG29Male
DOG51Male
DOG30Male
DOG44Male
DOG35Male
DOG57Male
DOG50Male
DOG40Female
DOG45Male
DOG26Male
DOG71Female
DOG87Female
DOG56Female
DOG75Male
DOG21Male
DOG68Male
DOG21Male
DOG22Male
DOG58Male
DOG55Male
DOG56Male
DOG52Male
DOG61Male
DOG35Male
DOG63Female
DOG57Male
DOG54Female
DOG31Female
DOG24Male
DOG24Male
DOG21Male
DOG57Female
DOG21Male
DOG27Male
DOG36Male
DOG23Male
DOG43Male
DOG24Male
DOG21Male
DOG34Male
DOG19Female
DOG46Male
DOG46Female
DOG45Female
DOG45Male
DOG29Male
DOG30Male
DOG25Male
DOG13Female
DOG41Male
DOG24Female
DOG17Male
DOG11Female
DOG25Male
DOG21Female
DOG39Female
DOG35Male
DOG70Female
DOG37Female
DOG31Male
DOG2Female
DOG28Female
DOG16Female
DOG82Male
DOG42Male
DOG20Male
DOG32Female
DOG39Female
DOG56Male
DOG66Male
DOG21Male
DOG53Female
DOG26Male
DOG28Male
DOG27Male
DOG30Female
DOG31Male
DOG19Male
DOG38Male
DOG55Male
DOG21Male
DOG46Male
DOG48Female
HORSE32Male
HORSE45Male
HORSE41Male
HORSE34Male
HORSE34Male
HORSE37Male
HORSE42Male
HORSE33Male
HORSE45Male
HORSE45Male
HORSE45Male
HORSE45Male
HORSE45Male
HORSE34Male
HORSE41Male
HORSE37Male
HORSE33Male
HORSE39Male
HORSE42Male
HORSE32Male
HORSE32Male
HORSE34Male
HORSE34Male
HORSE42Male
HORSE37Male
HORSE33Male
HORSE34Male
HORSE39Male
HORSE32Male
HORSE32Male
HORSE33Male
HORSE33Male
HORSE33Male
HORSE33Male
HORSE37Male
HORSE42Male
HORSE34Male
HORSE39Male
HORSE42Male
HORSE42Male
HORSE52Male
HORSE52Male
HORSE38Male
HORSE41Male
HORSE37Male
HORSE34Male
HORSE33Male
HORSE42Male
HORSE33Male
HORSE33Male
HORSE40Male
HORSE42Male
HORSE35Male
HORSE33Male
HORSE35Male
HORSE34Female
HORSE42Male
HORSE43Male
HORSE37Male
HORSE39Male
HORSE41Male
MICE13Female
MICE16Male
MICE16Male
MICE17Male
MICE17Male


ANIMALMaleFemale1-1011-20
BAT
BIRD
CAT
DOG
HORSE
MICE
 
thanks mate upon merging code is working! thanks mate!
You're welcome

As it is working then long term I would look at assigning variables to the ranges below outside of the loop as they are fixed ranges during the loop, and so don't need recalculating on each iteration of the loop.

VBA Code:
wsSource.Range("A2:A" & LRowSource)
wsSource.Range("B2:B" & LRowSource)
wsSource.Range("C2:C" & LRowSource)
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You're welcome

As it is working then long term I would look at assigning variables to the ranges below outside of the loop as they are fixed ranges during the loop, and so don't need recalculating on each iteration of the loop.

VBA Code:
wsSource.Range("A2:A" & LRowSource)
wsSource.Range("B2:B" & LRowSource)
wsSource.Range("C2:C" & LRowSource)
can i burden you a bit more if i may?
how can i compute or add to the "<11 header" all value that falls under a year?
ie. 11 month old, 3 month old
 
Upvote 0
thanks mate all good!
did some additional code and got it to count
VBA Code:
wsTarget.Range("I" & ctrX).value = WorksheetFunction.CountIfs(wsSource.Range("A2:A" & LRowSource), wsTarget.Cells(ctrX, "A"), wsSource.Range("B2:B" & LRowSource), "<11") + WorksheetFunction.CountIfs(wsSource.Range("A2:A" & LRowSource), wsTarget.Cells(ctrX, "A"), wsSource.Range("B2:B" & LRowSource), "*months*")
 
Upvote 0

Forum statistics

Threads
1,226,013
Messages
6,188,421
Members
453,473
Latest member
bbugs73

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