To use countif in a dynamic column using dynamic row range

Vimaldsouza

New Member
Joined
Sep 9, 2018
Messages
3
I have a data set of employees with their Present absent details. I want to use countif of A and P in the last column of the data set. I am not sure in which column the last data will be present as sometimes we take 2 months data etc. after finding the last column I want to add a coutif in all the cells row wise for each employee. Eg of data set is as below
Employee No. Name 1st 2nd 3rd 4th 5th 6th
1 Ap P P A P WO WO

2 Ba A A P P P P

Please help
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
As you are only counting non empty cells you can use any range as long as it's larger than the actual one =countif(1:1,"p") will always work
 
Upvote 0
As you are only counting non empty cells you can use any range as long as it's larger than the actual one =countif(1:1,"p") will always work


Thanks for your reply but I wanted this in VBA 1:1 needs to be dynamic and also the row end is unknown
 
Upvote 0
Code:
Sub CountingPresents()


Dim lastcol As Integer
Dim lastrow As Double


lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
lastrow = Cells(Rows.Count, 1).End(xlUp).Row


Range(Cells(2, lastcol + 1), Cells(lastrow, lastcol + 1)).Select


Selection.FormulaR1C1 = "=COUNTIF(RC[" & (-lastcol + 2) & "]:RC[-1],""p"")"


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,123
Members
452,546
Latest member
Rafafa

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