A function to count a column IF... :)

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
I have sheet with Column A as the Date and Column B which is a value picked from a list.

I'm trying to create a function that scans down Column A and if it locates TODAY() it checks that Row in Column B to see if there is a value from the list. In my perfect utopia it continues scanning down Column A until it is blank.

My psuedo code process I suspect will be something like this...

Code:
Function LDcpt()

Set LDcount = 0
Scan down Column A 
If scan of A = TODAY()
	Then Check this row Column B
		If B=LD 
			Then LDcount=LDcount + 1
Repeat scan down Column A until Column A is Empty.
When Column A is Empty end Scan

End Function

Any suggestions?

Sincerely,
WageSlave101
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
Originally I used two additional Columns one with;

Code:
=IF($A1=TODAY(),COUNTIF(B1,"LD"),"")

Then I would SUM the results of that Column into another cell. Which is what got me thinking that if I could write a function to do it, that would be alot cleaner.

I've been reading and digging but so far I'm not really sure where to go with this.

Sincerely,
WageSlave101
 

ravishankar

Well-known Member
Joined
Feb 23, 2006
Messages
3,566
Hi

Paste the following codes in the macro window ( Alt F11)

Code:
Sub Wage()
Cells(1, 1) = "=today()"
b = 0
x = Cells(Rows.Count, 1).End(xlUp).Row
For a = 2 To x
If Cells(a, 1) = Cells(1, 1) And Cells(a, 2) <> "" Then
b = b + 1
End If
Next a
MsgBox " There are " & b & " matches for today withvalues in B"
End Sub
Keep A1 free and Run the macro.
Ravi
 

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
Thanks for that, I'm not sure if I'm copying and pasting correctly ... I did find that if I used;

Code:
=COUNTIFS(A1:A501, TODAY(), B1:B501, "LD")

Gave me the correct answer.

That said I'd still like to work out a way to do it as a Sub or Function since if there's more than say 500 records then I need to adjust the code.

Sincerely,
WageSlave101
 

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
So I've been reading and experimenting... so far I have managed to run a Sub to count the rows until it reaches a blank cell.

Code:
Sub CurrentRows()
a = 1
Range("A1").Select
Do Until IsEmpty(ActiveCell.Value)
ActiveCell.Offset(1,0).Select
a = a + 1
Loop
MsgBox "There are " & a & " Rows"
End Sub

So the question now is it possible to nest the
Code:
=IF($A1=TODAY(),COUNTIF(B1,"LD"),"")
in there somewhere or is there a way to then use this with the previous COUNTIFS so that it replaces the;

Code:
=COUNTIFS(A1:Aa, TODAY(), B1:Ba, "LD")

Sincerely,
WageSlave101
 

Forum statistics

Threads
1,181,055
Messages
5,927,863
Members
436,573
Latest member
CMR237

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
Top