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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,214,378
Messages
6,119,188
Members
448,873
Latest member
jacksonashleigh99

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