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

wageslave101

Board Regular
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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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

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

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")``

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

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

Replies
2
Views
630
Replies
1
Views
371
Replies
12
Views
1K
Replies
2
Views
370
Replies
2
Views
314

1,219,792
Messages
6,150,291
Members
450,949
Latest member
faizanmalik10

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.

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

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