Dynamic Cell Address on Countifs inside For Loops

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
201
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Good day mate!
Need help to make my row criteria dynamic

VBA Code:
    For iCtr = 2 To LastRow
        ActiveSheet.Range("F" & iCtr).Formula = "=SUM(COUNTIFS('XVList'!$E$2:$E" & LRow & ",$A2,'XVList'!$H$2:$H" & LRow & ",""Female""))"    
    Next iCtr

how can I make dynamic entry for ",$A2,", what my idea is to make it like ,A & iCtr, or something to that effect, but running it does not count frequency
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Fixed it.... :)
Wondering how?

If you do the whole column at once instead of one row at a time it is both quicker/more efficient and Excel will automatically make the A2 dynamic.
Also, the SUM function is doing nothing in that formula so can be removed.
It would be something like this
Excel Formula:
Range("F2:F" & LastRow).Formula = "=COUNTIFS('XVList'!$E$2:$E" & LRow & ",$A2,'XVList'!$H$2:$H" & LRow & ",""Female"")"
 
Upvote 0
Wondering how?

If you do the whole column at once instead of one row at a time it is both quicker/more efficient and Excel will automatically make the A2 dynamic.
Also, the SUM function is doing nothing in that formula so can be removed.
It would be something like this
Excel Formula:
Range("F2:F" & LastRow).Formula = "=COUNTIFS('XVList'!$E$2:$E" & LRow & ",$A2,'XVList'!$H$2:$H" & LRow & ",""Female"")"

my code mate, which "fixed" my previous problem
VBA Code:
Range("F" & iCtr).Formula = "=SUM(COUNTIFS('XVList'!$E$2:$E" & LRow & ",$A" & iCtr & ",'XVList'!$H$2:$H" & LRow & ",""Female""))"

remove sum function and tried your code, indeed that realy worked!
really appreciate it mate! but would you be kind enough to add counting empty or 0 value
i.e.
if Male then Add one to Male Column
if Female then Add one to Female Column
if Sex = Empty (or null or no value at all) then Add one to Male Column

and follow up question if you still have the time mate, why did A2 became dynamic? that baffled me!
 
Upvote 0
would you be kind enough to add counting empty or 0 value
i.e.
if Male then Add one to Male Column
if Female then Add one to Female Column
if Sex = Empty (or null or no value at all) then Add one to Male Column
Have not seen any data or layout so have no idea what "Male Column" or "Female Column" is.
Also do not know if you mean add one for every male or just add one at the end.
What about some small dummy sample data & expected results with XL2BB and explain again in relation to that sample data?

why did A2 became dynamic?
It is just how vba & Excel work. Like asking if you put the formula =A1 in cell B1 and then copy B1 down the column, why does A1 become A2 then A3 etc in the formula? It just does. :)
 
Upvote 0
Have not seen any data or layout so have no idea what "Male Column" or "Female Column" is.
Also do not know if you mean add one for every male or just add one at the end.
What about some small dummy sample data & expected results with XL2BB and explain again in relation to that sample data?


It is just how vba & Excel work. Like asking if you put the formula =A1 in cell B1 and then copy B1 down the column, why does A1 become A2 then A3 etc in the formula? It just does. :)

ok mate, if sData.Sex is empty then count empty to Male Column of said Specie in tData.Male, using our formula above (countifs)

SPECIESex
American flamingoMale
AlcidsFemale
Amazon kingfisherMale
AlbatrossesFemale
AlbatrossesMale
Accipiter hawksFemale
Accipiter hawksMale
American dipperFemale
Accipiter hawksMale
AlbatrossesFemale
American flamingoMale
AlbatrossesFemale
AlbatrossesMale
Accipiter hawksFemale
American dipperMale
AlcidsFemale
AlbatrossesMale
Albatrosses
Accipiter hawks
AlbatrossesFemale
Accipiter hawksMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
Accipiter hawksFemale
Accipiter hawksMale
Accipiter hawksFemale
Accipiter hawksMale
Accipiter hawksFemale
AlbatrossesMale
AlbatrossesFemale
Accipiter hawksMale
Accipiter hawksFemale
American dipperMale
American kestrelFemale
American white pelicanMale
American white pelicanFemale
American white pelicanMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
AlcidsMale
Accipiter hawksFemale
Accipiter hawksMale
Accipiter hawksFemale
American dipperMale
American white pelicanFemale
Accipiter hawksMale
Accipiter hawksFemale
American dipperMale
American dipperFemale
American kestrelMale
American white pelicanFemale
American white pelicanMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
Accipiter hawksFemale
Accipiter hawksMale
Accipiter hawksFemale
Accipiter hawksMale
American kestrelFemale
AlbatrossesMale
American dipperFemale
American white pelicanMale
American dipperFemale
AlcidsMale
Accipiter hawksFemale
American dipperMale
American kestrelFemale
American kestrelMale
American white pelicanFemale
American white pelicanMale
AlcidsFemale
Accipiter hawksMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
Accipiter hawksMale
Accipiter hawksFemale
American dipperMale
American white pelicanFemale
American white pelicanMale
American white pelicanFemale
American white pelicanMale
American white pelicanFemale
AlcidsMale
Accipiter hawksFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
Accipiter hawksFemale
Accipiter hawksMale
AlbatrossesFemale
AlbatrossesMale
Accipiter hawksFemale
Accipiter hawksMale
AlcidsFemale
AlbatrossesMale
Accipiter hawksFemale
American white pelicanMale
American white pelicanFemale
AlcidsMale
Amazon kingfisherFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
AlbatrossesFemale
Accipiter hawksMale
Accipiter hawksFemale
Accipiter hawksMale
Amazon kingfisherFemale
AlbatrossesMale
AlbatrossesFemale
AlbatrossesMale
Accipiter hawksFemale
Accipiter hawksMale
Accipiter hawksFemale
AlbatrossesMale
AlbatrossesFemale
American kestrelMale
AlbatrossesFemale
AlcidsMale


SpecieMaleFemale
Alcids
Amazon kingfisher
Albatrosses
Accipiter hawks
American dipper
American kestrel
American white pelica
 
Upvote 0
It is just how vba & Excel work. Like asking if you put the formula =A1 in cell B1 and then copy B1 down the column, why does A1 become A2 then A3 etc in the formula? It just does. :)
stupid me! :) just like dragging cell value across range! got it mate :)
 
Upvote 0
Looks like you missed this bit
some small dummy sample data & expected results

Also, can you use Mini Sheet instead of Table Only so we can see what columns/rows are actually involved? You appear to have given adjacent column but originally it was non-adjacent columns E & H.
Also, what is sData and tData - neither mentioned or shown before?

Please remember that while your worksheets and data are familiar to you, we know nothing about them apart from what you specifically tell or (better) show us.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,114
Members
452,302
Latest member
TaMere

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