Can someone turn this equation into a custom function?

Darranimo

Board Regular
Joined
Jan 19, 2022
Messages
52
Office Version
  1. 365
Platform
  1. Windows
I am trying to learn custom functions and could use help with this formula:

Excel Formula:
=IF(E$2="ACTUAL",COUNTIFS(tblClsd[udAbbr],$B12,tblClsd[ClsDate],"<="&E$5,tblClsd[ClsDate],">"&D$5),"")

I am embarrassed to show my attempt but here it is anyway:

VBA Code:
Function CountClosedDeals(udAbbr As String, clsDateStart As Date, clsDateEnd As Date, actual As String) As Integer
Dim tbl As ListObject
Dim abbr As Range
Dim clsd As Range
Set tbl = Worksheets("tblClsd").ListObjects("tblClsd")
Set abbr = tbl.ListColumns("udAbbr").DataBodyRange
Set clsd = tbl.ListColumns("ClsDate").DataBodyRange

If actual = "ACTUAL" Then

CountClosedDeals = Application.WorksheetFunction.CountIfs(abbr, udAbbr, clsd, "<=" & clsDateEnd, clsd, ">" & clsDateStart)

Else
CountClosedDeals = ""
End If
End Function

If you can show me where I went wrong I would really appreciate it!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
This is not VBA, but a LAMBDA that you create the function in the name manager and then call the function.
In the worksheet snippet, I test the Lambda by "seeding" the parameters after the function.
The CALC in F8 is standard Excel Formula.
The CALC in F10 is my Lambda making/testing
The CALC in F12 is actually using the custom LAMBDA function.

Mr Excel Questions.xlsx
ABCDEFGHIJ
1
2ACTUAL
3tblClsd
4Date 1Date 2ClsDateudAbbr
51/20/20231/29/20291/16/2023DOMDZ
61/17/2023RTYPE
71/17/2023KIIMQ
811/18/2023SYMHU
91/19/2023PCEME
10MAKING LAMBDA:11/19/2023ZVZEX
111/20/2023VHRIB
12an AbbrZVZEXCount_Abbr_in_Date_Range:11/21/2023ZVZEX
131/22/2023HESGN
141/23/2023HVYQU
15
Sheet18
Cell Formulas
RangeFormula
F8F8=IF(E$2="ACTUAL",COUNTIFS(tblClsd[udAbbr],$B12,tblClsd[ClsDate],"<="&E$5,tblClsd[ClsDate],">"&D$5),"")
F10F10= LAMBDA(Status,Date1,Date2,Abbr,colDates,colAbbrs, IF(Status="ACTUAL",COUNTIFS(colAbbrs,Abbr,colDates,">="&Date1,colDates,"<="& Date2),""))(E2,D5,E5,B12,I5:I14,J5:J14)
F12F12=Count_Abbr_in_Date_Range(E2,D5,E5,B12,I5:I14,J5:J14)
 
Upvote 1
Solution
Assuming you are practicing making a UDF then try changing your If statement to the below:
- the Ucase is a just in case. It would be better to have a variable name that is more obviously a variable
- the main change is adding CLng to convert the dates. Dates can be a bit tricky

Oh by the way, you are more likely to get a response by following @awoohaw's lead and posting an XL2BB sample of you data. It often takes as long to set up some test data as coming up with a solution.

Rich (BB code):
If UCase(actual) = "ACTUAL" Then
    CountClosedDeals = Application.WorksheetFunction.CountIfs(abbr, udAbbr, clsd, "<=" & CLng(clsDateEnd), clsd, ">" & CLng(clsDateStart))
Else
    CountClosedDeals = ""
End If
 
Upvote 1
This is not VBA, but a LAMBDA that you create the function in the name manager and then call the function.
In the worksheet snippet, I test the Lambda by "seeding" the parameters after the function.
The CALC in F8 is standard Excel Formula.
The CALC in F10 is my Lambda making/testing
The CALC in F12 is actually using the custom LAMBDA function.

Mr Excel Questions.xlsx
ABCDEFGHIJ
1
2ACTUAL
3tblClsd
4Date 1Date 2ClsDateudAbbr
51/20/20231/29/20291/16/2023DOMDZ
61/17/2023RTYPE
71/17/2023KIIMQ
811/18/2023SYMHU
91/19/2023PCEME
10MAKING LAMBDA:11/19/2023ZVZEX
111/20/2023VHRIB
12an AbbrZVZEXCount_Abbr_in_Date_Range:11/21/2023ZVZEX
131/22/2023HESGN
141/23/2023HVYQU
15
Sheet18
Cell Formulas
RangeFormula
F8F8=IF(E$2="ACTUAL",COUNTIFS(tblClsd[udAbbr],$B12,tblClsd[ClsDate],"<="&E$5,tblClsd[ClsDate],">"&D$5),"")
F10F10= LAMBDA(Status,Date1,Date2,Abbr,colDates,colAbbrs, IF(Status="ACTUAL",COUNTIFS(colAbbrs,Abbr,colDates,">="&Date1,colDates,"<="& Date2),""))(E2,D5,E5,B12,I5:I14,J5:J14)
F12F12=Count_Abbr_in_Date_Range(E2,D5,E5,B12,I5:I14,J5:J14)
Give me your brain, please! lol Thank you!!
 
Upvote 0
happy to help.
Although I respect VBA, I try to use worksheet formula solutions first. Particularly if the people using the workbooks are unfamiliar with VBA. That does have a downside though, as some folks may tinker and break worksheet formulas!
 
Upvote 0

Forum statistics

Threads
1,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

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